I've created the server REST API to return a response with HTTP code and a JSON (or XML) object with more details: application code (specific to scenario, message that describe what happened etc.).
So, for example, if a client sends a Register request and the password is too short, the response HTTP code will be 400 (Bad Request), and the response data will be: {appCode : 1020 , message : "Password is too short"}.
In jQuery, I'm using the "AJAX" function to create a POST request. When the server returns something different from HTTP code 200 (OK), jQuery defines it as "error".
The error handler can get 3 parameters: jqXHR, textStatus, errorThrown. How can I get the JSON object that is sent by the server in an error case?
Here is my JavaScript code:
function register (userName, password) { var postData = {}; postData["userName"] = userName; postData["password"] = password; $.ajax ({ dataType: "json", type: "POST", url: "<server>/rest/register", data: postData, success: function(data) { showResultSucceed(data); hideWaitingDone(); }, error: function (jqXHR, textStatus, errorThrown) { showResultFailed(jqXHR.responseText); hideWaitingFail(); } }) }When looking at Firebug console, it seems like the response is empty. When invoking the same request by using REST testing tool, I get a response with JSON object it it.
What am I doing wrong?
Acceptmade it fail to return the correct response body on a 400 error.)