Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Ajax - how to get response data in error

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?

Edit:

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) {             showResultSucceed(data);             hideWaitingDone();         },         error: function (jqXHR, textStatus, errorThrown) {              showResultFailed(jqXHR.responseText);             hideWaitingFail();         }     }) } 

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?

like image 984
Roy Tsabari Avatar asked Jan 30 '12 16:01

Roy Tsabari


People also ask

How does error handle response in ajax?

When there is an AJAX error response or the AJAX request times out, you'll want to log as much information as you have, including the error message that jQuery gives you, the url and the request data. $. ajax(url, { "data": requestData, "type": "POST", "timeout": 5000 }) .

How do I return a response text in ajax?

What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete): somefunction: function(callback){ var result = ""; myAjax = new Ajax.

How can I alert ajax response data?

ajax({ url:"myscript. php", dataType: "json", success:function(data){ alert(data. cenas); } });


2 Answers

directly from the docs

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader()

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

In the link above, look for the section entitled

The jqXHR Object

like image 29
hvgotcodes Avatar answered Oct 04 '22 06:10

hvgotcodes


Here's an example of how you get JSON data on error:

$.ajax({     url: '/path/to/script.php',     data: {'my':'data'},     type: 'POST' }).fail(function($xhr) {     var data = $xhr.responseJSON;     console.log(data); }); 

From the docs:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

Otherwise, if responseJSON is not available, you can try $.parseJSON($xhr.responseText).

like image 163
mpen Avatar answered Oct 04 '22 06:10

mpen