Ajax with JQuery: 200 ok, but not “success”

Scenario: Ajax request always return 200 OK but error event is fired instead of success. In this scenario We have a code that returns exception from controller/code behind and UI Ajax call has to handles those exception.When you specify: dataType: ‘json’, jQuery will fire the error event if the response cannot be parsed as JSON, even if server returns 200 OK. Check the data returned from the server and make sure it is valid JSON (try JSONLint service).If the returned data is not JSON or it has syntax errors then fix them in your server side code. You can just return {} from the server side script. The best shot will be wrap the response object with the status and error code/message and handle it at UI layer for example duplicate record or invalid file type etc.

var URL = 'https://ajax.googleapis.com/ReviewUploadfiles;
$.ajax({
    type: 'GET',
    url: URL,
    dataType: 'json',
    success: function( data, status ){
      if (data.responseData != null && data.responseData.Success) {
      alert(' File Uploaded!' );
}
else{
     alert(data.result.Errormessage);
}
    },
    error: function() {
        alert( 'Something goes wrong!' );
    }
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s