0

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?

4

1 回答 1

2

error函数需要一个回调函数,但您正在执行console.log(jqxhr)并尝试将其返回值error作为回调传递,这就是您“未定义 jqxhr”错误的来源。

你想要这样的东西:

$.get('site.php', function(data, status, jqxhr) function(data) {
    //...
}).error(function(jqXHR, textStatus, errorThrown) {
    // Do something interesting.
});

您还应该使用fail而不是erroras erroris going away

弃用通知:jqXHR.success()、和jqXHR.error()回调jqXHR.complete()将在 jQuery 1.8 中弃用。要为最终删除准备您的代码,请使用jqXHR.done(),jqXHR.fail()jqXHR.always()

于 2011-11-18T06:08:09.107 回答