1

I'm sending a query to a server and awaiting a JSON object in return:

var a = $.ajax(
{
    type: "POST",
    data: post_data,
    timeout: 5000,
    url: url,
    beforeSend: function (xhr)
    {
        xhr.setRequestHeader("Content-Type", "application/json"); 
    }
})
.success (function (data, status, jqxhr)
{       
    onSuccess();
})
.error (function(x, status, jqxhr) 
{   
    onError_(x);
});

That works perfectly, I'm getting the expected JSON in return:

{"readyState":4,"responseText":"","status":201,"statusText":"Created"}

But not onSuccess() but onError_() gets called and status is set to 'parsererror' despite the return is a valid JSON (I've tested it with various tools like this) and even the status of the JSON element is 201 which according to this page represents a positive status code:

10.2.2 201 Created

The request has been fulfilled and resulted in a new resource being created [...]

Is it possible, that jQuery's Ajax interprets the 201 status as a failure?

Update:

Also adding dataType: "json", to my request doesnt change the situation. The jQuery documentation for .ajax() says

dataType (default: Intelligent Guess (xml, json, script, or html))

So when I implemented it I thought jQuery wouldn't be so dumb to cause errors because it doesn't recognize JSON and it seems that I was right because the error remains the same.

Update:

The common fix for this problem seems to be, adding dataType: "json" to the ajax call, however this didn't work for me, so I did an ugly but working workaround:

.error (function(x, status, jqxhr) 
{
    if (x.status == 201) /* jQuery thinks, Status 201 means error, but it doesnt so we have to work around it here */
    {
        // handle success
        return;
    }

    // handle errors
}

Anyway, I'd be still interested in a proper fix

Community
  • 1
  • 1
Tom Doodler
  • 1,471
  • 2
  • 15
  • 41
  • possible duplicate of [Data inserted successful but jquery still returning error](http://stackoverflow.com/questions/2233553/data-inserted-successful-but-jquery-still-returning-error) – Jamiec Sep 02 '15 at 10:23
  • Two possible causes for your behaviour, 1) the server returns a different status code for the HTTP response (not the one specified inside the JSON data, but the one in the HTTP status line. 2) you don't specify a dataType in your ajax request and the server does not set the correct MIME type for the response. – Marco Sandrini Sep 02 '15 at 10:24
  • The `dataType: "json"` doesn't fix the problem, I updated my question – Tom Doodler Sep 02 '15 at 11:01
  • Probably you did already, but did you doublecheck that the server really returns what you are expecting? – Marco Sandrini Sep 02 '15 at 11:26
  • Yes, because 1. everything works fine with the workaround and 2. the server hosts a telecommunication system of a very big company so I dont think that they release a version which isn't working if you have to pay thousands of euros for it. Anyway, it works so the return is correct – Tom Doodler Sep 02 '15 at 11:29
  • Do you have a way of dumping the whole response (including headers)? Or alternatively try adding a header like "Accept: application/json" to your request... – Marco Sandrini Sep 02 '15 at 11:33

2 Answers2

4

Be careful: ResponseText is equal to "" and that is not a valid json. It should be null or "{}"

pinturic
  • 2,253
  • 1
  • 17
  • 34
0

Also, write dataType: "json" in ajax call.

Harsh Aggarwal
  • 182
  • 1
  • 7