简体   繁体   English

在通过JQuery进行Ajax调用期间,如何处理服务器端异常?

[英]How do I handle a server side exception during an Ajax call from JQuery?

I am calling a Page Method from JQuery as described here: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/ 我正在从JQuery调用页面方法,如下所述: http : //encosia.com/2008/05/29/using-jquery-to-direct-call-aspnet-ajax-page-methods/

This is all working fine. 一切正常。 During testing I came across an error when my response from the Web Method was too large: Error during serialization or deserialization using the JSON JavaScriptSerializer. 在测试期间,当我从Web方法得到的响应太大时,遇到一个错误:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。 The length of the string exceeds the value set on the maxJsonLength property. 字符串的长度超过了在maxJsonLength属性上设置的值。

I was able to do a bit of research on the maxJsonLength property and set it higher, however I would like to try to catch this exception serverside, before it is sent to the client during the Ajax call. 我能够对maxJsonLength属性进行一些研究并将其设置为更高,但是我想在Ajax调用期间将其发送到客户端之前尝试捕获此服务器端异常。 How do I do this? 我该怎么做呢?

I have set a try/catch block within my method to no avail, it is happening outside my method (during JSON Serialization). 我在我的方法中设置了一个try / catch块无济于事,它发生在我的方法之外(在JSON序列化期间)。

StackTrace looks like this: StackTrace看起来像这样:

at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)  
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)  
at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)  
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

Update : 更新
The global and page level catches won't work for me (but will probably get the bounty at this point. Maintaining a global exception for something very specific to one page call seems like a poor design decision.) The point of me wanting to catch the exception is so I can trunkcate the text and still make a valid response to the javascript call. 全局和页面级别的捕获对我来说不起作用(但可能会得到赏金。对于一个页面调用非常特定的内容,保持全局异常似乎是一个糟糕的设计决策。)我想捕获的点唯一的例外是,我可以对文本进行中继,并且仍然可以对javascript调用做出有效的响应。

It might be a great effort, but if a well-designed solution is what you're after, I'm thinking your best bet is probably to create a class that extends the service handler, that implements your own custom serializer, where you check for maxJsonLength. 可能需要付出很大的努力,但是如果您要使用经过精心设计的解决方案,那么我想最好的选择可能是创建一个扩展服务处理程序的类,该类实现您自己的自定义序列化程序,您可以在其中检查对于maxJsonLength。 You could then setup your web.config to let all (or some) asmx requests be handled by your own handler. 然后,您可以设置web.config以使所有(或某些)asmx请求都由您自己的处理程序处理。 The Spring framework seems to facilitate this, although I haven't looked closer into that solution. 尽管我没有仔细研究该解决方案,但Spring框架似乎有助于实现这一点。

Be aware, though, that manually truncating a JSON string, without breaking the syntax, could be quite an effort in itself. 但是请注意,在不破坏语法的情况下手动截断JSON字符串本身可能是一项很大的工作。

Another solution, which would perhaps be easier to implement, and still arguably pretty neat, would be to overwrite (or wrap) jQuery's $.ajax function with a function of your own, that stringifies the JSON on the client (this is done anyway, to create the request in the first place, so it shouldn't be a lot of overhead), and checks length there. 另一个解决方案可能更易于实现,而且仍然可以说很简洁,可以用您自己的函数覆盖(或包装)jQuery的$.ajax函数,该函数将客户端上的JSON字符串化(无论如何,首先创建请求,因此应该不会有太多开销),并在那里检查长度。 The gain with this approach is that you don't have to mess with extending the service handler, but also, the part of truncating the object would be easier, as your javascript object is readliy at hand; 这种方法的好处在于,您不必为扩展服务处理程序而烦恼,而且,由于您的javascript对象已准备就绪,因此截断对象的部分会更容易。 you can iterate it, stringify subsets of it, check their length, or use wahtever mechanism you like, to implement smart truncating (obviously whatever means of truncating JSON you want to use, you'll have to know your data, to be able to decide what can safely be removed without breaking key functionality in the request). 您可以对其进行迭代,对其子集进行字符串化,检查其长度,或者使用自己喜欢的机制来实现智能截断(显然, 无论您想使用哪种截断JSON的方法,您都必须了解数据,才能确定可以安全删除的内容而不破坏请求中的关键功能)。

If truncating cannot be done, you'd have the option of displaying an error message, without even having to hit the server. 如果无法进行截断,则可以选择显示错误消息,而不必点击服务器。

Since javascript is so flexible, you could specify, say, $.ajaxCore = $.ajax , and then overwrite $.ajax with your own function, that calls $.ajaxCore . 由于javascript非常灵活,因此您可以指定$.ajaxCore = $.ajax ,然后使用自己的函数$.ajaxCore覆盖$.ajax

也许如何:处理页面级错误会有所帮助。

您也可以尝试在应用程序中添加全局异常处理程序。

Calculate object size yourself, before returning from method. 从方法返回之前,自己计算对象的大小。 You don't need to do exact calculation, just an estimate would be fine. 您无需进行精确的计算,只需估算即可。 Most likely it would be enough to get size of text fields that could be large (eg description of something or comments). 最有可能获得足够大的文本字段(例如,某物的描述或评论)。

If result size is bigger than certain limit, truncate the data. 如果结果大小大于特定限制,则截断数据。 Set MaxJsonLength twice as big as your limit. 将MaxJsonLength设置为限制的两倍。

如果您无法访问实际引发异常以对其进行修复的序列化程序的基础代码,也许可以选择使用自定义错误处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM