4

Updated: I'm posting HTML FORM data but expecting to receive JSON data. I am not trying to POST JSON data.

I am trying to get a JSON response back from doing a HTML FORM POST request. I have successfully received a JSON back when using a simple HTML FORM POST request (i.e. not AJAX). My JSON response from the HTML FORM POST is this:

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

The problem occurs when I try to handle the request and response with jQuery's .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 running the above code and debugging in Firebug, it appears that the POST request is going through, but something is going wrong on the handling of 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 appears that the 31 bytes of data is being sent. However, when debugging the actual Javascript, the error function gets called and the xhr object is this:

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

I know the jQuery.ajax() document states that "In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown." However, I believe my JSON is valid as I have checked it at jsonlint.com.

What else could be going wrong?

3
  • Try adding contentType: "application/json; charset=utf-8" to $.ajax() Commented Jul 27, 2011 at 17:31
  • var data1 = "apikey="+apikey+"&firstname="+fName+"&lastname="+lName+....... I know data1 is not hte problem because the server is successfully receiving the data Commented Jul 27, 2011 at 17:32
  • Adding contentType: "application/json; charset=utf-8" causes the POST request to not go through to the server successfully. I'm not POSTing JSON data. I'm POSTing simple FORM data, but the response is in JSON. Commented Jul 27, 2011 at 17:36

4 Answers 4

1

It looks to me like you are getting a server error. I would check the status code of the response and fix whatever is causing the request to fail on the server.

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

1 Comment

Using Firebug, the status code is 200 and the response headers are what I wrote above. The response headers show up in Firebug in the "Headers" tab, but the "Response" tab shows nothing. The header indicates that there will be 31 bytes of data which is how much I am expecting. However, I put breakpoints in the success and error functions of the jQuery code, and the code stops in the error function, with the xhr object as show above in my original post.
0

your getting an error because data1 is not formatted in json, so when it receives the data it gets a parse error. data1 needs to be formatted:

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

6 Comments

I'm not sending JSON. I'm trying to receive JSON... Sorry, maybe I wasn't clear in the original post.
I understand, but you said your server is throwing a json parse error.
I've checked the server and it successfully receives and stores the POST data that I sent (from data1). Are you sure that the xhr object being { readyState=0, status=0, statusText="error"} in the Firebug debugger means that the server is throwing a json parse error? I think that there is something wrong with my jQuery .ajax() code, not the server.
take out the line dataType: "json", i use ajax a lot but i never pass json as the datatype
possibly you need to define {"success":true,"data":1234567} as an object on the server side and then pass the object itself to the callback.
|
0

I was having the same problem. It seems that this is an issue with Cross Domain.

Finding this SO answer: https://stackoverflow.com/a/7605563/154513

helped me.

Comments

0

Some times Jquery return Internal Error 500 for currectly data.

There is example for read the same json data withour error

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://reqres.in/api/products/3", true);
xhr.onload = function(){
    console.log(xhr.responseText);
};
xhr.send();

1 Comment

And check for a wrong sumbols after echo result inbackend.

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.