Here is how I am submitting my ajax request :
$.ajax({
url: "myurl",
type: 'POST',
dataType : "text",
data : ({
json : myjson
}),
success : function(data) {
if(data == 'success'){
//alert('success');
}
else {
alert('fail');
}
};
}
});
Within java im just returning the string which contains either ‘success’ or ‘fail’ depending on wether or not a exception is thrown :
pseudo code :
try {
return "success";
}
catch (Exception e){
return "fail";
}
Will this suffice instead of using the ajax error callback ? If not sufficient how should this callback be used and what extra failure conditions is it checking for ?
error : function() {
alert ('error');
}
The purpose of
errorcallback is different: jQuery will call that function if an error occurred during sending request or receiving a response. For example, if your server doesn’t return an answer but instead “500 Internal Server Error” (or any other error for that matter), thensuccesscallback function will not be called at all. If you define theerrorcallback, then it will be called – and you’ll have some chance of figuring out what the error was.Just because your server works perfectly doesn’t mean that a communication error cannot occur: for example, it could be “Gateway timeout”, “No route to host”, etc.
Finally, if you declare
dataType: json(I know it doesn’t apply in your example) but the returned data cannot be parsed (i.e. it’s not a well-formatted JSON string), yourerrorfunction will be called and not yoursuccessfunction.