This is a confusing one for me. When I run an ajax request it’s always returning an error, but checking the response the data in the responseText is actually the data I need. So why is it reaching the error?
$.ajax({
type: "GET",
url: "/getjson.php?name=jay",
dataType: "json",
success: function(msg){
alert(msg);
},
error: function(msg) {
alert("Error: " + msg.responseText);
}
});
The returned HTTPStatus code is what jQuery uses to determine which handler to call.
If the code is in the 2 hundred range (200 – 299) or 304 then a call to the success callback will be made, otherwise the error callback will be called.
Also, if jQuery fails to parse the response, it is treated as an error (eg if html is returned when json is expected, the error callback will be called)