Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax call returning an error with readystate 4, status 200, statustext ok

This is really boggling my mind. I get an error callback from ajax. But if I take the res.responseText (which comes back correct, btw) from the error message and use it, it does the right thing. Just as if I had received a success callback.

The data is set like this:

var dataToSend = {fieldname : textdata};

and the ajax call is like this:

var ajaxOptions = {
    url: '/newpage',
    data: JSON.stringify(dataToSend),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    cache: false,
    processData: false,
    type: 'POST',
    success: function(res) {
        console.log("success!");
        $('#' + divname).html(res);
    },
    error: function(res) {
        console.log("There was an error: " + JSON.stringify(res));
        $('#' + divname).html(res.responseText);
    }
};

$.ajax(ajaxOptions);

The error message is : There was an error: {"readyState":4,"responseText" [this part is perfectly fine], "status":200, "statusText":"OK"}.

like image 970
lynvie Avatar asked Sep 17 '15 18:09

lynvie


People also ask

What is readyState in Ajax?

AJAX - Server Response The readyState property holds the status of the XMLHttpRequest. The onreadystatechange property defines a function to be executed when the readyState changes. The status property and the statusText property holds the status of the XMLHttpRequest object.

What does readyState 0 mean?

From W3schools: readyState=0. Means that the request isn't sent. (your broswer isn't connected to the targeted server). It's mean that the socket is not opened : no TCP handshake, so the targeted URL is just not reached...


2 Answers

If your responseText isn't a correct JSON, a parsing error is thrown. Either make sure your response is a valid JSON or remove dataType: "json".

From jQuery docs:

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

Type: String

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

...

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

like image 149
Diego Avatar answered Sep 22 '22 17:09

Diego


This happens to be when you have dataType set to get the response but the response is not what you set in the datatype.

So in your case dataType: 'json', is set and as you mentioned at the comment section you have set string at the backend, so you need to change your datatype to text.

change your datatype to:

dataType: 'text',
like image 44
Jai Avatar answered Sep 26 '22 17:09

Jai