0

Ok, here's the problem. I've an api that will give me back a json. With success I'll have back a nice list with some values, with an error I'll receive a json containing sone information about the error. I still haven't tried the "success" case because I still have to deploy the api but I think should work. The problem for now is the "error" case. Using $.ajax in case of error I'm going to receive three parameters, a xhrequest (or similar), a message and I don't remember.. How can I handle this? In the response body shoulde be a json, where can I find it?

Thanks for any help, sorry for no code or the poor indentantion but I'm writing from my mobile!

4
  • 2
    If your API is returning allegedly useful content in the body of an "error" (4xx) response then it's designed wrong Commented Jul 5, 2012 at 19:31
  • The error code set up is 500. Commented Jul 5, 2012 at 19:35
  • I rest my case - 5xx error codes are supposed to relate to server errors (e.g. lack of resources, etc) and not for application layer errors. Commented Jul 5, 2012 at 19:40
  • Yes, I'm not saying that you're wrong, of course an internal server error should represent an "internal server error" but it was like a "requirement" let's say. Commented Jul 5, 2012 at 19:44

3 Answers 3

1

The error callback gets called when it can't get the response from the server (like a 404 error or the like). If you want to pass back your own errors, you need to put them in the JSON response and test for them in your success callback.

For instance, in a success callback I'm working on right now:

    jQuery.ajax({
        'url':      'Client/saveClient.mas',
        'dataType': 'json',
        'data':     {
            'first_name'            :   first_name,
            'middle_initials'       :   middle_initials,
            'last_name'             :   last_name,
            'phone_number'          :   phone_number
            },
        'success':  saveClientSuccessCallback,
        'error':  saveClientFailureCallback
    });
  }
}

function saveClientSuccessCallback(json)
{
  if (json.status == 'error')
  {
    updateClientTips(json.error_msg);
  }
  else
  {
    addReadOnlyClient(json.people.values, json.manager_uperson_id);

    jQuery('#new-client-form').dialog("close");
  }
}
function saveClientFailureCallback(jqXHR, textStatus, errorThrown)
{
  updateClientTips(textStatus + ': ' + errorThrown);
}

As you can see, I return an error condition in json.status and the message in json.error_msg.

Sign up to request clarification or add additional context in comments.

1 Comment

This looks similar on what we're doing. Thanks.
0

The actual body of the response should be available in jqxhr.responseText

3 Comments

Thanks, I'll check it asap and let you know.
Hm.. there's something that I don't get.. if I use responseText I cannot see anything. Also debugging I can see that the responseText is "" (empty). But if I simply go to the url that I'm using I can clearly see the response. Any help?
I've just discovered that was a problem of Chrome while running a local server... crazy... thanks btw.
0

As Paul Tomblin said, you have (at least) two options:

  • Setting an error code for your response results in error being called. In this case, the datatype of the response content is usually nothing to work with, so it is not parsed and put into a data variable. But you may access the response text via jqXHR.responseText and parse the JSON yourself (.parseJSON()).

  • Returning a 200response with proper JSON content, which is parsed automatically into a data object (if dataType is set to json). You can then check if(data.error) {...}, get the error message and present it to the user.

I would go for the second option, but it depends on which errors you are expecting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.