I have a RESTful app that returns HTTP 400 with an JSON-encoded reponse body when it encounters application errors:
Request URL: http://.../middleware.php/entity/23.json?padding=jQuery20305847647329799524_1380182255277&_=1380182255278
Request Method: GET
Status Code: HTTP/1.1 400 Bad Request
Server: Apache/2.2.14 (Ubuntu)
Date: Thu, 26 Sep 2013 07:57:36 GMT
Content-Type: application/json
Content-Length: 133
Response body is valid JSONP according to FF:
jQuery20305847647329799524_1380182255277({"version":"0.2","exception":{"message":"Invalid UUID: '23'","type":"Exception","code":0}});
I'm trying to handle the failure:
$.getJSON("http://demo.volkszaehler.org/middleware.php/entity/23.json?padding=?").fail(
function(jqXHR, textStatus, errorThrown) {
console.error(JSON.stringify(jqXHR));
console.error(jqXHR.responseText);
}
);
Unfortunately, the jqXHR has only the following properties, both on FF25 and Chrome:
{"readyState":4,"status":404,"statusText":"error"}
The responseText itself shows as "" and I'm unable to decode the action JSON response body.
How can the actual JSON response body be parsed in the fail() method?
Note: I've seen How do I catch jQuery $.getJSON (or $.ajax with datatype set to 'jsonp') error when using JSONP? but it seems to relate to catching the error at all, not about handling the error responses content.
<script>element. I assume if you set a response code other than200, the script won't be executed by the browser. I don't think there is a solution other than changing the response code, at least for JSONP requests.<script>element to the document and the browser downloads and executes the script. You don't have access to the actual request object. jQuery just provides a simplified version of the request object for consistency.