Take the 2-minute tour ×

I have a simple web application. I've created the server REST API so it will 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 send 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. Ho can I get the JSON object that sent by the server in error case?


1) Here is my JS 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) {
        error: function (jqXHR, textStatus, errorThrown) {


2) 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?

share|improve this question
In case you don't make it as far down as the accepted answer; there is nothing wrong with this code, the problem lies elsewhere. –  Tony Jan 16 '13 at 21:22
add comment

2 Answers

directly from the docs


so use the jqXRH argument and get the responseText property off it.

In the link above, look for the section entitled

The jqXHR Object

share|improve this answer
I read that. the responseText is empty –  Tibo Jan 30 '12 at 16:11
@gdoron added the link and section to look for –  hvgotcodes Jan 30 '12 at 16:13
@tibo, then show us your code. Something is off. Might be best to pause execution in your handler with firebug or webkit and just look at the properties of the arguments. It's gotta be there somewhere... –  hvgotcodes Jan 30 '12 at 16:14
I just added my code to the question. Can you help? –  Tibo Jan 31 '12 at 8:05
without actually digging in, I'm not sure at this point. Are you sure the response body is empty in firebug? Are you sure the url is correct? Are you sure the error handler is being called? –  hvgotcodes Jan 31 '12 at 12:59
show 1 more comment
up vote 2 down vote accepted

After spending so much time on this problem, I found the problem.

The page is under the URL: www.mydomain.com/register
The REST api is under the URL: server.mydomain.com/rest

Seems like this kind of POST is not so simple.
I'm going to search more information to understand this issue better (if you have more information please share it with me).

When putting the REST API under www.mydomain.com/rest - everything is working fine.

share|improve this answer
add comment

Your Answer


