我如何处理jquery中的多个ajax错误 [英] how can i handle multiple ajax errors in jquery

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

问题描述

我希望我的错误函数以特定的方式工作,以便它应显示与未加载的json文件相对应的错误.当我单击任何相应的链接业务",能源"等时,我希望错误函数显示诸如business.json not loadedEnergy.json not loaded之类的消息.

I want my error function to work in a specific way such that it should display error corresponding to the json file that is not laded. I want error function to display massages like business.json not loaded, Energy.json not loaded, accordingly when I click on any of the corrisponding link "business", "energy", etc.

key变量保存每个json文件的值.例如,如果我单击页面上的业务"链接,它将具有business.json

The key variable holds the value for each json file. For example it would have business.json if i click "business" link on my page

我的代码是:

$.ajax({
    url: "./"+key + "?"+ new Date().getTime(),
    dataType: 'json',
    success: function(courses){
        currentResults=courses;
        populateSubCat();
        $("#kss-spinner").css({'display':'none'});
    },
    error: function(key) {  
        //console.log(xhr.Status);

        if(key != "business.json"){
            console.log('business.json not loaded ');
        }
    }
});

推荐答案

尝试一下:

jQuery Ajax错误处理功能

$(function() {
    $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
});

在调用ajax函数之后,您将获得错误详细信息.

After that call your ajax function, you will get the error details.

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

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