I have a simple ajax post to the server..
$(".invite-team-members-submit-btn").click(function() {
$.post("invite_team_member", {
token: $("#token").val(),
email: $("#email").val(),
team: $("#team").val()
},"json")
.done(function (responseText) {
alert(responseText.response);
})
.fail(function (xhr,status,message) { alert("ERROR: " + message); })
.then(function () { alert("Something should happen."); });
});
The JSON returned looks like this...
{"response":"Person has been invited."}
My response header in the console looks like this...
Response Headers
Cache-Control max-age=0, private, must-revalidate
Connection close
Content-Type application/json; charset=utf-8
Date Wed, 22 May 2013 21:45:07 GMT
Etag "e5b5e12acbcc78372b2a861027b66c05"
Status 200 OK
Transfer-Encoding chunked
X-Request-Id d835ce021eff7733d67ebfcdd468bdf2
X-Runtime 0.007909
x-ua-compatible IE=Edge
My console is not only telling me that I received a 200 status but that my response includes the JSON I want. However when it comes to handling the parsed JSON no alerts are made. Note that I'm receiving NO alerts, not even from the then function. What could be causing this?
console.log(responseText)andconsole.log(typeof responseText)Since you are returning the application/json content-type header, jQuery may be parsing it for you, where it then later errors on jQuery.parseJSON because you're trying to parse an object rather than a string.parseJSON