masterofchant
masterofchant

Reputation: 55

jquery ajax issue - error even if status code is 200

I am trying to execute jQuery Ajax request with the following code:

$.ajax({
    url: '<MY_URL>',
    type: 'POST',
    contentType: 'application/json;charset=UTF-8',
    data: JSON.stringify(data),
    dataType: 'application/json',
    success: getParsedData,
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status + ' ' + $.parseJSON(xhr.responseText['message']);
    },
    xhrFields: {
        withCredentials: true
    }
});  

Instead of executing function specified in success variable, I am getting alert popup with value 200 (which is valid request).

In developer tools in the browser I can also see:

Remote Address: ...
Request URL: ...
Request Method: POST
Status Code:200 OK

Can you please advise how to troubleshoot/handle it further to have success/error functions to be executed correctly.

Upvotes: 3

Views: 2701

Answers (1)

Joseph
Joseph

Reputation: 119847

I believe dataType for JSON is simply json.

jQuery also fires the error handler when the data doesn't match what is expected. For instance, returning an HTML or malformed JSON to an AJAX request that expects JSON can also trigger the error handler, even when the response status is a non-error status.

Upvotes: 5

Related Questions