JQuery does not handle JSON response from AJAX POST

Updated: I am sending HTML FORM data, but expect to receive JSON data. I am not trying to get POST JSON data.

I am trying to get a JSON response from an HTML FORM POST request. I successfully got JSON back when using a simple HTML FORM POST request (i.e. Not AJAX). My JSON response from HTML FORM POST:

{"success":true,"data":1234567} 

The problem occurs when I try to process the request and response using jQuery.ajax ().

 $.ajax({ type: "POST", url: URL, data: data1, dataType: "json", success: function(data, textStatus, jqXHR) { alert ("success"); }, error: function(xhr, status, error) { alert ("Error: " + error); } }); 

After executing the above code and debugging in Firebug, it seems that the POST request passes, but something is wrong when processing the response. Firebug tells me the following regarding the HTTP response from the POST request:

 Response Headers Cache-Control private Content-Length 31 Content-Type application/json; charset=utf-8 ... 

So it seems that 31 bytes of data are being sent. However, when debugging the actual Javascript, the error function and the xhr object are called:

 Object { readyState=0, status=0, statusText="error"} 

I know that the jQuery.ajax () document states that "In jQuery 1.4, JSON data is processed strictly, any rejected JSON is rejected and a parsing error occurs." However, I believe my JSON is valid as I tested it on jsonlint.com.

What else could be wrong?

+4
source share
3 answers

It seems to me that you are getting a server error. I would check the response status code and fix everything that caused a request error on the server.

+1
source

you get an error because data1 is not formatted in json, so when it receives data, it gets a parsing error. data1 needs to be formatted:

 data1={"apikey":apikey, "firstname":fName } 
0
source

I had the same problem. This seems to be a problem with Cross Domain.

Search for this SO answer: fooobar.com/questions/1364444 / ...

helped me.

0
source

All Articles