在 jquery ajax 请求不成功时做某事

do something at unsuccessful jquery ajax request

我的 ajax 请求遇到了具有速率限制的 api。

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "jsonp",
    dataObj : index,
    success: function (response, textStatus, xhr) {
        console.log('success');
    }
    ,error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    }
});

我想知道我什么时候达到速率限制。

但对于在控制台中显示此内容的请求:

Failed to load resource: the server responded with a status of 429 (OK)

我没有看到 'success' 或 'error'。 好像success和error都没有执行。

有没有办法弹出和提醒,例如?

我试过complete,但也没用。

谢谢

我找不到 dataObj 的文档,但删除它似乎可以使查询 运行 正确。

如果删除它,error 回调将被执行,您将能够在控制台中看到 error

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "jsonp",
    success: function (response, textStatus, xhr) {
        console.log('success');
    }
    ,error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    }
});

编辑: 原来你真正想要改变的是数据类型。除非您还明确传递回调,否则您应该告诉 jQuery 您得到的是 json 而不是 jsonp

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "json",
    dataObj: index,
    success: function (response, textStatus, xhr) {
        console.log('success');
    }
    ,error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    }
});

尝试在设置中使用 jQuery statusCode 对象,如下所示:

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "jsonp",
    dataObj : index,
    success: function (response, textStatus, xhr) {
        console.log('success');
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    },
    statusCode: {
      429: function() {
        alert( "Exceeded request limit." );
      }
    }
});