I have an ajax call passing data to a page which then returns a value.
I have retrieved the successful call from the page but i have coded it so that it raises an error in the asp. How do i retrieve that error from the jquery?
For example:
cache: false,
url: "addInterview_Code.asp",
type: "POST",
datatype: "text",
data: strData,
success: function (html) {
alert('successful : ' + html);
$("#result").html("Successful");
},
error: function (error) {
**alert('error; ' + eval(error));**
}
It's the error bit that I don't understand. In the function what parameter do I need to put, so that I can then use the error message that I raised in the server.
Best Answer
The required parameters in an Ajax
error
function arejqXHR, exception
and you can use it like below:DEMO FIDDLE
Parameters
jqXHR:
Its actually an error object which is looks like this
You can also view this in your own browser console, by using
console.log
inside theerror
function like:We are using the
status
property from this object to get the error code, like if we get status = 404 this means that requested page could not be found. It doesn't exists at all. Based on that status code we can redirect users to login page or whatever our business logic requires.exception:
This is string variable which shows the exception type. So, if we are getting 404 error,
exception
text would be simply 'error'. Similarly, we might get 'timeout', 'abort' as other exception texts.So, in case you are using jQuery 1.8 or above we will need to update the success and error function logic like:-
Hope it helps!