user2064000 user2064000 - 4 months ago 10
Ajax Question

How do I get the response text of an XHR in jQuery?

I have the following jQuery snippet:

$.get('/foo', 'a=foo&b=bar', function () {
// ...
});


The response object can be accessed by using
this
in the closure. However, I need to get the response text returned by the XHR. How do I go about doing this?

(BTW, since my use of "XHR" is apparently causing some confusion here, I'd like to clarify that this is not a XMLHttpRequest object but rather a jQuery object, which I've already verified.)

Answer

You rewrite the success-function like this:

$.get('/foo', 'a=foo&b=bar', function (data, textStatus, jqXHR) { 
    console.log(jqXHR.responseText);
});

For details, see https://api.jquery.com/jquery.get/

Comments