如何获取有关jquery ajax错误的更多详细信息? [英] How to get more details regarding jquery ajax error?

查看:92
本文介绍了如何获取有关jquery ajax错误的更多详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的示例ajax代码.

This is my sample ajax code.

$.ajax({
    type: 'POST',
    url: validateAjaxURL,
    success: function (data) {                  
        var returnData = data;  
        if (returnData.match("^selectedUno-")) {                                
              $('#new_caregiver_popup_div').dialog('close');    
        } else {
            $("#new_caregiver_popup_div").html(data);
        }
    },
    error: function() {                 
        alert('Unable to contact server');
    }
});

这很好.但是,如果发生错误,则只会发出无法联系服务器"警报,因此我想获取有关该错误的更多详细信息,例如哪种类型的错误,错误原因等.我该怎么办? >

This is working fine. But if an error occurs it will only give the alert "Unable to contact server", so I want to get more details about the error such as what type of error, reason for the error etc. How can I do that?

推荐答案

我通常会这样写"error"函数:

I usually write the "error" function something like this:

function (jQXHR, textStatus, errorThrown) {
    alert("An error occurred whilst trying to contact the server: " + jQXHR.status + " " + textStatus + " " + errorThrown);
}

这是根据 http://api.jquery.com/jQuery.ajax/.您可以阅读3个参数以了解更多信息.根据错误的性质,有时它比其他错误消息提供更多信息.

This is as per the documentation at http://api.jquery.com/jQuery.ajax/. There are 3 parameters you can read from in order to find out more. Depending on the nature of the error, sometimes it's more informative than others.

还取决于您的浏览器,在控制台中查看和在开发人员工具(大多数浏览器中为F12)中的网络日志记录也会为您提供一些线索.

Also depending on your browser, looking in the console and the networking logging in the developer tools (F12 on most browsers) may also give you some clues.

这篇关于如何获取有关jquery ajax错误的更多详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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