AJAX : How to get info about permission reject - ajax

I have an ASP.NET MVC application, where I call method by AJAX:
$.ajax({
dataType: 'json',
url: "/Admin/AllowVideoUpload",
type: "POST",
data: { val: val },
error: function () {
alert('Error');
}
});
if success - nothing happens on client side, if error - get 'Error' message. But error can be by different reasons - problem inside "AllowVideoUpload" method or user lost his credentials (AdminController has attribute 'Authorize(Roles = "Admin")'). I want to differ these 2 types of error. How to do it?

Those are your application level errors. So it should be handled in the success handler.
You may return json from your action method which has a Status /Error code/Message element where you mention what is the error reason.
Sample Json for Error
{
"Status": "Failed",
"Message": "Authentication Failed"
}
Sample Json for Success
{
"Status": "Success",
"Message": "Successfully Updated"
}
Read the jSon and then you can decide what to do next.may be show message to user
$.ajax({
dataType: 'json',
url: "/Admin/AllowVideoUpload",
type: "POST",
data: { val: val },
success:function(data){
if(data.Status=="Success")
{
//operation success. do whatever
}
else if(data.Status=="Failed")
{
alert(data.Message);
}
},
error: function () {
alert('Error');
}
});

Related

KendoUI Datasource Schema.Errors not firing when the server returns 400

When the server returns 200 my schema.errors function is called with the server response as a parameter so that I can look through the response and return any found errors. However, my server returns code 400 when there is an error and it seems that kendo fails when it detects a 400 prior to calling my errors function. Is this behavior intentional? Is there a way to make the kendo error handling function work as expected on http codes other than 200?
dataSource: {
transport: {
read: {
dataType:'json',
url:'/contents'
},
create: {
dataType:'json',
type:'POST',
url:'/contents'
},
},
schema: {
errors: function(response) {
console.log('check error', response);
}
}
}

controller is not geting view data

function export(){
$.ajax({ url: "#Url.Content("~/controllr/method")",
type: 'GET',
data: { selectedValue : $("#BranchId option:selected").text() },
traditional: true,
async:false,
success: function (result ) {
},
failure: function () {
failed=true;
alert("Error occured while processing your request");
},
error: function (xhr, status, err) {
failed=true;
alert("Error occured while processing your request");
}}
Did you try setting a breakpoint on the controller action and see whether its is being hit ? Please try the following basic code and see whether the call is getting through
$.ajax({
url: ‘#Url.Content(“~/MyController/MyMethod”)’,
type: ‘post’,
data: {
selectedBranchId : $("#BranchId option:selected").text()
}
});
// I am the controller action
public ActionResult MyMethod(string selectedBranchId)
{
// your code
}
Please note I have dropped all the callback code and changed the controller name and the action name with the name of the postback variable

ajax error not refreshing

My .jsp page partial code
function runAjax(){
$.ajax({
type: "POST",
url: "Test.html",
data: { testparam1,testparam2},
success: function(data){
document.getElementById("processdata").innerHTML="Success";
},
error: function(e){
document.getElementById("processdata").innerHTML="Error";
}
});
}
In my controller that handles the post, i raise an error on purpose for testing. The client code shows the message 'Error'. With same session, i refresh my page and run it so that the error is not raised. INstead of "Success" I still see 'Error" even though i know the controller worked as expected.
should i be doing this
function runAjax(){
$.ajax({
type: "POST",
url: "Test.html",
data: { testparam1,testparam2},
success: function(data){
document.getElementById("processdata").innerHTML="Success";
},
error: function(request){
document.getElementById("processdata").innerHTML="Error";
}
});
}
and then in the controller
try{
...
}catch(Exception e)
{
response.getWriter().print("An error occured");
}
If in my original code the error is being cached, how do I clear it each time my controller is called?

Ajax Call with PUT method

i am trying to make ajax call with PUT method. Below is the code, but i am getting with the error XML Parsing Error: no element found Location: moz-nullprincipal:{c847a4af-f009-4907-a103-50874fcbbe35} Line Number 1, Column 1:
$.ajax({
type: "PUT",
async: true,
url: "http://localhost:8080/karthick/update",
data: JSON.stringify(params),
contentType: "application/json",
dataType: "JSON",
processdata: true,
success: function (json) { //On Successfull service call
},
error: function (xhr) {
alert(xhr.responseText);
}
});
return false;
};
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
But this service is working Good with Rest-client jar. Also my POST method works fine in my browser. Please help me in this.
Regards
Karthick
Usually, this error comes, when making a cross browser request. Try data: JSONP and see if it helps.

How to make an AJAX request to jsFiddle's echo api using Ext.Ajax?

I'm trying to make a simple AJAX call to jsFiddle's JSON echo api using Ext.Ajax. It makes the request successfully, but the object returned is empty, not the JSON object I send (which it should be).
Code:
Ext.Ajax.request({
url: '/echo/json/',
jsonData: { foo: 'bar' },
success: function(resp) {
console.log('success!');
console.log(Ext.decode(resp.responseText)); //empty object..? why?
},
failure: function(resp) {
console.log('failure!');
},
});
Fiddle: http://jsfiddle.net/nANE7/
How come the response is just an empty object? Where's the foobar JSON information that it's meant to echo back?
Try this:
Ext.Ajax.request({
url: '/echo/json/',
params: {
json: Ext.encode(
{
param1:'bar'
}
)
},
success: function(resp) {
console.log('success!');
console.log( resp );
console.log(Ext.decode(resp.responseText)); //empty object..? why?
},
failure: function(resp) {
console.log('failure!');
},
});

Resources