抓住jQuery的阿贾克斯错误回调错误? [英] Catch errors in jquery ajax error callback?

查看:130
本文介绍了抓住jQuery的阿贾克斯错误回调错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于使用了很多jQuery的AJAX调用服务器端在我的应用程序。

I tend to use a lot of jquery ajax calls to the server side in my applications.

通常,当不顺心的事在服务器端生病序列化errormessage的,并发送一个响应(JSON)。类似的东西,以

Usually when something goes wrong on the server side ill serialize an errormessage and send as a response (JSON). Something similar to

 { "ErrorMessage" : "Something went wrong: " + ex.message }

我想知道的是,如果有什么办法可以使错误最终在jQuery的阿贾克斯错误的回调,而不是成功

有没有办法做到这一点?或者我应该坚持的错误处理我的老路上?如果你提供了一个PHP或ASP.NET + C#示例,因为即时通讯intrested在这两个没关系。谢谢

Is there any way to do this? Or should I stick to my old way of handling errors? It doesn't matter if you provide a PHP or ASP.NET + c# example, since im intrested in both. Thanks

推荐答案

可以让他们结束了在jQuery的的错误回调。在ASP.NET中,所有你需要做的是在web.config中自定义错误部分更改为<的customErrors模式=关 /> 如果你走这条路线,确保你把你的Web服务在一个单独的文件夹,这样你只能做它为您的Web服务调用,而不必关闭这对于整个网站;例如:

You can make them end up in the error callback on jQuery. In ASP.NET, all you need to do is to change the custom errors section in your web.config to <customErrors mode="Off" /> BUT if you go this route, make sure that you put your web service in a separate folder so that you only do it for your web service calls without turning off this for the entire site; for example:

<location Path="/Services"> <!--Your web service lives here -->
    <system.web>
        <customErrors mode="Off" />
    </system.web>
</location>

这样一来,扔在你的web方法的任何异常都将在jQuery中错误回调处理。

您可以让不会对您的Web方法缓存它除了传播或者你可以捕捉它,并重新抛出一个更加用户友好的消息。

You can either let the exception propagate without caching it on your web method or you can catch it and rethrow a more "user friendly" message.

这篇关于抓住jQuery的阿贾克斯错误回调错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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