如何获取Ajax调用的默认错误 [英] How to get default error of ajax call

查看:86
本文介绍了如何获取Ajax调用的默认错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我要做的是根据错误代码警告ajax错误,并且我在网站上有很多ajax调用,所以我正在使用全局ajax错误处理程序功能.

Ok, what I am trying to do is alerting ajax errors according to its error codes and I have lots of ajax calls on website so I am using global ajax error handler function.

但是我想要的是,如果一些ajax调用已经具有默认错误,然后显示那里不是全局的.

But what I want is if some ajax call already have default errors then show there not global.

$(document).ready(function(){
    $(document).ajaxError(e,xhr,opt){
        if(xhr.error){
            //Don't do anything
        } else {
            alert('You have an error');
        }
    }
}

第一个功能:

$.ajax({
    type:"post",
    url:"page.php",
    data:"name=mohit&lastname=bumb",
    error:function(){
        alert('error');
    }
});

第二功能:

$.ajax({
    type:"post",
    url:"page.php",
    data:"name=mohit&lastname=bumb",
});

因此,在第二种情况下,它应该显示您有错误,在第一种情况下,只需显示错误

So in 2nd case it should show You have an error and in first case just error

推荐答案

是的,但是您必须重写jQuery默认的$.ajax方法.检查以下我在我的项目之一中使用的代码.确保在jQuery之后加载脚本.

Yes you can, but you have to override jQuery default $.ajax methods. Check the following code that I used in one of my projects. Make sure you load the script just after jQuery.

我的情况是-

  1. 该网站有很多ajax部分视图,这些视图必须检查用户是否登录.因此,我不得不重写jquery调用进行检查.

  1. The web site had a lot of ajax partial views which had to check whether user is logged in or not. So I had to override jquery calls to check for it.

在进行任何ajax调用时,我还必须显示一个加载程序.

I also had to show a loader when any ajax call was made.

还有一件事,ajax加载了一些js,所以我添加了一个检查该URL是.js文件还是普通URL.

One more thing, some js are loaded by ajax, so I added a check whether the url is a .js file or normal url.

我已经删除了对我的项目保密的敏感代码.其余的在这里.这可能会对您有所帮助.

I have taken out the sensitive codes that were confidential for my project. The rest is here. This might help you.

$(document).ready(function () {
    var oldjQuery = [];
    oldjQuery["ajax"] = $.ajax;
    oldjQuery["load"] = $.load;
    var newOptions = [];
    //override ajax
    jQuery.ajax = function (options) {
        newOptions["ajax"] = $.extend({}, options);
            //override the success callback
            newOptions["ajax"].success = function (data, textStatus, jqXhr) {
                try {
                    if (options.url.indexOf('.js') <= -1) {
                        //this is a normal success call, do nothing
                    }
                }
                catch (err) {
                         //... my other codes, incase any error occurred
                }

                if (typeof options.success != 'undefined') { 
                        //the ajax call has a success method specified, so call it
                        options.success(data, textStatus, jqXhr);
                    }
            };

            //override the error callback
            newOptions["ajax"].error = function (jqXhr, textStatus, errorThrown) {
                try {
                    if (options.url.indexOf('.js') <= -1) {
                    //this is a normal success call, do nothing
                    }
                }catch (y) {
                    //... my other codes, incase any error occurred
                }

                //the ajax call has an error method specified, so call it
                if (typeof options.error != 'undefined') {
                    options.error(jqXhr, textStatus, errorThrown);
                }
            };

            return oldjQuery["ajax"](newOptions["ajax"]);
        };

        //override load function
    jQuery.load = function (url, data, completeCallback, ignore) {
        newOptions["load"].completeCallback = function (d, textStatus, jqXhr) {
            try {
                if (url.indexOf('.js') <= -1) {
                    //codes
                }
            } catch (err) {
                try {
                    //codes
                }catch (err2) {
                }
            }

            if (typeof completeCallback != 'undefined') {
                    //call the default completed callback
                    completeCallback(d, textStatus, jqXhr);
                }
            };

            return oldjQuery["load"](url, data, newOptions["load"].completeCallback);
        };
});

这篇关于如何获取Ajax调用的默认错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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