如何为所有的AJAX调用处理特定的HTTP错误? [英] How to handle specific HTTP error for all AJAX calls?

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

问题描述

我有一个web应用程序,要求,并通过AJAX发送数据,而作为回应,我的服务器端发送HTTP状态codeS,视情况而定。因此,例如,如果用户试图登录,而他的记录,我可能返回 400 HTTP状态code 。最终我与警报处理它,等等。

但处理这些HTTP状态codeS变得太沉重,因为我大量使用AJAX的。这意味着我会反复处理HTTP状态code与每一个AJAX请求,这将导致重复code,这是一个不好的做法。

因此​​,我在找什么是一种能够在一个地方处理这些错误,所以我只是处理所有 400 401 等使用相同的code。

我目前在做什么:

手工处理的错误为每个AJAX调用。通过使用 $的状态code 。阿贾克斯()

 状态code:{
        500:功能(数据){
            警报(一些友好的错误消息放在这里。');
        }
 

这似乎是一个矫枉过正对我来说,我的web应用程序开发,并为我创造更多的Ajax调用。我会一次又一次地重复这片code。

目前,我想到的唯一的想法是创建,将工作在AJAX之上的功能,是这样的:

 函数doAjax(类型,URL,数据,moreVars){
//这个功能仅仅是一个简单的例子,可以更复杂和灵活。
        $阿贾克斯({
            类型:类型,
            网址:网址,
            数据:数据,
            moreOptions:moreVars,
            //现在处理所有的状态code。
            状态code:{
                //从一个地方处理所有HTTP错误。
            }
        });
    }

    doAjax(POST,mydomain.com/login.php,dataObj);
 

解决方案

您可以使用 $。ajaxSetup() 注册全局错误状态的处理程序。

  

说明:为未来的Ajax请求设置默认值

例如:

  $。ajaxSetup({
    状态code:{
        500:功能(数据){
            警报(一些友好的错误消息放在这里。');
        }
    }
});
 

I have a web app, requesting and sending data via AJAX, and as a response, my server-side sends HTTP status codes, depending on the situation. so for example if a user tries to login while he's logged I probably return a 400 HTTP status code. And eventually i handle it with an alert, etc.

But handling these HTTP Status codes gets too heavy, since I'm making heavy use of AJAX. that means I'll be handling HTTP status code repeatedly with every AJAX request, which will result in duplicated code, and that's a bad practice.

So, what I'm looking for is a way to handle all these errors in one place, so I just handle all 400, 401, etc with the same code.

What i'm currently doing:

Handling the errors manually for each AJAX call. By using the statusCode in$.ajax().

  statusCode: {
        500: function(data) {
            alert('Some friendly error message goes here.');
        }

It seems like an overkill for me, as my web app develops, and as I create more ajax calls. I'll be repeating this piece of code again and again.

Currently, the only idea I have in mind is creating a function that will work on top of AJAX, something like:

    function doAjax(type,url, data, moreVars) {
//this function is just a SIMPLE example, could be more complex and flexible.
        $.ajax({
            type: type,
            url: url,
            data: data,
            moreOptions:moreVars,
            //now handling all status code.
            statusCode: {
                //handle all HTTP errors from one place.
            }
        });
    }

    doAjax("POST", 'mydomain.com/login.php', dataObj);

解决方案

You can use $.ajaxSetup() to register global error state handlers.

Description: Set default values for future Ajax requests.

Example:

$.ajaxSetup({
    statusCode: {
        500: function(data) {
            alert('Some friendly error message goes here.');
        } 
    }   
});

这篇关于如何为所有的AJAX调用处理特定的HTTP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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