防止未处理的jQuery AJAX错误 [英] Prevent Unhandled jQuery AJAX Error

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

问题描述

我知道如何接收服务器数据或错误。有很多方法例如:

  $。ajax({
type:get,
url:小部件/,
成功:function(data,text){
console.log('this is a data');
},
error:function(request,status ,错误){
console.log('这是一个错误');
}
});

请看这张图片:



< img src =https://i.stack.imgur.com/Soc3o.pngalt =result in chrome developer console>



我有我的错误功能我在那里做我需要的
如何防止第一行出现错误。
我已经测试了4xx和5xx错误,但没有区别。
我知道最终用户在隐藏开发者控制台时看不到此错误。



我需要设计一个API。
我知道我可以从服务器发送200个响应,附加的参数显示这是一个错误或成功的结果。



但是如果我可以发送4xx或5xx响应,那么就不需要额外的参数。
这样对我有一些额外的好处。例如,我可以将成功结果应用于客户端模型,而不用担心其他参数。



谢谢

解决方案

AFAIK在返回错误状态码(4xx,5xx)时不能让红色的东西消失。它们被浏览器用来指示某些东西可能没有像预期的那样消失。仍然这些只是装饰,因为4xx和5xx状态代码是完全有效的,所以我没有看到你想消除它们的原因。如您所说,您的另一种选择是从服务器发送200响应,并附加一个参数,表明这是一个错误或成功的结果。



/ em>



我当然建议使用4xx和5xx代码(这就是他们的设计),并不要担心浏览器在开发者控制台中的单挑警告(又名红色的东西)。



另请参见这个提到其他类型资源(图像,文件等)时引用类似错误的问题。你看到的错误实际上是一样的。


I know how to receive server data or error. There are many ways. For example:

$.ajax({
    type: "get",
    url: "/widgets/",
    success: function (data, text) {
        console.log('this is a data');
    },
    error: function (request, status, error) {
        console.log('this is an error');
    }
});

Please see this image:

I have my error function and I do what I need there. How can I prevent error in the first line. I have tested 4xx and 5xx errors but no difference. I know that end user does not see this error when developer console is hidden.

I need this to design an API. I know that I can send 200 responses from server with an additional argument that show that this is an error or a success result.

But if I can send 4xx or 5xx responses then there is no need to extra argument. This way has some additional gains for me. For example I can apply success result to my client side models without worry about additional args.

Thank you

解决方案

AFAIK you cannot make the red thingies go away, when returning error status-codes (4xx, 5xx). They are used by the browser to indicate that something might have not gone as expected. Still these are mere decorations, since 4xx and 5xx status codes are perfectly valid, so I don't see a reason for you wanting to eliminate them.

As you said yourself, your other alternative would be sending "200 responses from server with an additional argument that show that this is an error or a success result".

I certainly recommend using the 4xx and 5xx codes though (that's what they are designed for) and stop worrying about browser's heads-up warnings in developer console (aka red thingies).

See also this and this questions that refer to similar errors when fetching other kinds of resources (images, files etc). The errors you see are actually of the same nature.

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

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