在常规错误处理函数中获取失败的请求详细信息

时间:2015-07-28 10:36:09

标签: javascript jquery ajax

我正在使用ajaxSetup来处理所有错误:

$.ajaxSetup({
    error: handleErrors
});

var handleErrors = function(xhr) {
    console.log('this is ');
    console.log(this);
};        

在这种情况下,this是全局范围。

但是,如果我为每个调用都有一个单独的错误处理程序:

$.ajax({
    error: function(xhr){
        console.log('this is ');
        console.log(this);
    },  
    ...

this是已发送的ajax请求,包含url,type,contentType等。如何在我的常规handleErrors函数中访问该信息?所以如果它失败了,我特别知道失败的请求了吗?

2 个答案:

答案 0 :(得分:1)

  

function(data,textStatus,jqXHR){}

答案 1 :(得分:0)

JQuery文档建议使用ajaxError

您可以使用以下选项为其附加函数处理程序:

  

处理程序

     

jqXHR

thrownError将成为您的XHR对象。虽然require 'yaml' string =<<-END_OF_YAML :concurrency: 5 :pidfile: /tmp/pids/sidekiq.pid :logfile: log/sidekiq.log staging: :concurrency: 10 production: :concurrency: 20 queues: - default END_OF_YAML YAML.load(string) # { # :concurrency => 5, # :pidfile => "/tmp/pids/sidekiq.pid", # :logfile => "log/sidekiq.log", # "staging" => { # :concurrency => 10 # }, # "production" => { # :concurrency => 20 # }, # "queues" => [ # [0] "default" # ] # } 将是错误消息。

  

从jQuery 1.8开始,必须将此功能仅附加到文档中。