jQuery $ .ajax错误处理 [英] jQuery $.ajax error handling

查看:102
本文介绍了jQuery $ .ajax错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jQuery ajax错误函数

我试图替换全局ajax错误处理程序,由于我们的实现中观察到的问题,我可以从每个ajax调用中调用这个函数。

I am trying to replace a "global" ajax error handler with a function that I can call from each ajax call due to problems observed in our implementation.

$(document).ajaxError(function(e, jqXHR, settings, exception) {
    // using BrowserDetect function to get browser info
    var browser    = BrowserDetect.browser;
    var browserVer = BrowserDetect.version;
    var browserOS  = BrowserDetect.OS;
    var ajax_url   = settings.url;
    $.ajax({async: true, 
            type:     'POST',
            url:      AJAX_ERROR_LOG_URL, 
            dataType: 'json',
            data:     'host='myhost.com&status='+jqXHR.status+'&error='+jqXHR.responseText+'&expmessage='+exception.message+'&url=' 
                       +ajax_url+'&browser='+browser+'&browserVer='+browserVer+'&browserOS='+browserOS
    });
    if (jqXHR.status === 0) {
        final_message(ERROR_MSG + '0'); // Not connected. Please verify network is connected.
    } else if (jqXHR.status == 404) {
        final_message(ERROR_MSG + '404'); // Requested page not found. [404]
    } else if (jqXHR.status == 500) {
        final_message(ERROR_MSG + '500'); // Internal Server Error [500].
    } else if (exception === 'parsererror') {
        final_message(ERROR_MSG + '1'); // Requested JSON parse failed.
    } else if (exception === 'timeout') {
        final_message(ERROR_MSG + '2'); // Time out error.
    } else if (exception === 'abort') {
        final_message(ERROR_MSG + '3'); // Ajax request aborted.
    } else {
      if(browserVer == '7' && browser == 'Explorer') {
        final_message(ERROR_MSG + '100'); // Uncaught Error
      } else {
        final_message(ERROR_MSG + '99'); // Uncaught Error
      }
    }
});

我想将其转换为一个函数,并像下面这样调用:

I want to convert this to a function and call it like this:

$.ajax({async: true, 
        type:'POST',
        url: url, 
        dataType: 'json', 
        data:     "username="+username+"&password="+pass,
        success:  ajaxLoginResult
        error:    logAjaxError(jqXHR, textStatus, errorThrown)
});

function logAjaxError(jqXHR, textStatus, errorThrown) {
  // more code here to log the error
}

问题在于我无法获取ajax的url调用该错误回调的URL,因为唯一传递的是jqXHR,textStatus,errorThrown。在.ajaxError设置中,设置被传递,这就是我们从中获取url的地方。
任何想法?

The problem lies in the fact that I cannot get the url of the ajax callpassed to the error callback as the only things that are passed is the jqXHR, textStatus, errorThrown. In the .ajaxError setup, the settings is passed and that is where we get the url from. Any ideas?

编辑2/3:
这是Add_Course函数,您可以看到:

Edited 2/3: Here is the Add_Course function for you to see:

function Add_Course(reason) {
  var eventid = $('#eventid'+$('#obleventcode').attr('value')).val();
  var url = AJAX_URL_ADDTOCART + '?sku='+eventid+'&mainitem=true&pid='+pid;
  if(reason != 'VOUCHER'){
    log_url(url);
    $.ajax({async: true, 
            type:'POST',
            url: url, 
            dataType: 'json', 
            success: function(data){
              if(data.result=='success') {
                var cemsg_index=reason.indexOf('CEMSG');
                if(cemsg_index>=0) {
                  final_ce_message(reason.substr(cemsg_index+6));
                } // if(cemsg_index>=0)
              } else {
                $('#sm_content .oblcontent').html(data.message);
              } // if(data.result=='success')
            },
            complete: function(jqXHR, textStatus) {
              log_url('redirecting to cart');
              window.location = CART_URL;
            } // complete
    });
  }
}


推荐答案

恭喜实际检查错误。这么多人编写的代码,从来没有期望在互联网上出现问题。 Ha。

First, congratulations on actually checking for errors. So many people write code that never expect things to go wrong on the internets. Ha.

而不是编辑每个调用 $。ajax ,您可以调用 $。 ajaxSetup()让每个调用都使用你的函数来处理错误。错误处理程序签名是错误(jqXHR,textStatus,errorThrown),方便地匹配您的功能,所以尝试调用$ code $ $ ajaxSetup({error:在执行 $。ajax 调用之前,请先执行logAjaxError})。

Rather than editing every call to $.ajax you can call $.ajaxSetup() to have every call use your function for an error handler. The error handler signature is error(jqXHR, textStatus, errorThrown) which conveniently matches your function, so try a call to $.ajaxSetup({ error: logAjaxError }) before you do any $.ajax calls.

这篇关于jQuery $ .ajax错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆