3

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.

1
  • 2
    Use console.log(xhr, ajaxOptions, thrownError) to get better details. Commented Aug 12, 2015 at 21:08

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! after the change, everything start working as expected :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.