I’m writing a REST api. It returns a header status 400 on error.
However, on the AJAX side, I couldn’t get the jqxhr object returned.
$.get('site.php', function(data, status, jqxhr) function(data) {
//...
}).error(console.log(jqxhr));
it returns Uncaught ReferenceError: jqxhr is not defined.
In inspector console, its showing 400 (Bad Request) for the get request.
How do I get the text Bad Request?
I could return the text error within data by passing it in header status 200, but that’s not the right approach am I correct?
The
errorfunction wants a callback function but you’re executingconsole.log(jqxhr)and trying to hand its return value toerroras the callback, that’s where you’re “jqxhr is not defined” error comes from.You want something like this:
You should also be using
failinstead oferroraserroris going away: