如何在JQuery的Ajax调用期间处理服务器端异常?

时间:2022-11-29 14:03:15

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的Page方法,如下所述:http://encosia.com/2008/05/29/using-jquery-to-directly-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. The length of the string exceeds the value set on the maxJsonLength property.

这一切都很好。在测试期间,当我从Web方法的响应太大时遇到错误:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过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. How do I do this?

我能够对maxJsonLength属性进行一些研究并将其设置得更高,但是我想在Ajax调用期间将它发送到客户端之前尝试捕获此异常服务器端。我该怎么做呢?

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.

更新:全局和页面级别捕获对我来说不起作用(但可能会在此时获得赏金。维护一个非常特定于一个页面调用的全局异常似乎是一个糟糕的设计决策。)我想要的点捕获异常是因为我可以对文本进行trunkcate并仍然对javascript调用做出有效响应。

5 个解决方案

#1


3  

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. You could then setup your web.config to let all (or some) asmx requests be handled by your own handler. The Spring framework seems to facilitate this, although I haven't looked closer into that solution.

这可能是一项很大的努力,但是如果一个设计良好的解决方案就是您所追求的,我认为您最好的选择是创建一个扩展服务处理程序的类,它实现您自己的自定义序列化程序,您可以在其中检查对于maxJsonLength。然后,您可以设置web.config以让所有(或某些)asmx请求由您自己的处理程序处理。 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. 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; 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).

另一种解决方案,可能更容易实现,并且仍然可以说是非常简洁,可以用你自己的函数覆盖(或包装)jQuery的$ .ajax函数,在客户端上对JSON进行字符串化(无论如何,这样做,首先创建请求,因此它不应该是很多开销),并在那里检查长度。使用这种方法的好处是你不必乱用扩展服务处理程序,而且,截断对象的部分会更容易,因为你的javascript对象在手边可读;你可以迭代它,字符串化它的子集,检查它们的长度,或者使用你喜欢的wahtever机制来实现智能截断(显然无论你想要使用什么方法截断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,然后用自己的函数覆盖$ .ajax,调用$ .ajaxCore。

#2


2  

Maybe How to: Handle Page-Level Errors will help.

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

#3


2  

You can also try to add a global exception handler in your app .

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

#4


1  

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 (e.g. 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设置为限制的两倍。

#5


0  

If you can't access the underlying code of the serializer which actually throws the exception to fix it, maybe custom error handlers are an option?

如果您无法访问实际抛出异常来修复它的序列化程序的底层代码,那么自定义错误处理程序可能是一个选项吗?

#1


3  

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. You could then setup your web.config to let all (or some) asmx requests be handled by your own handler. The Spring framework seems to facilitate this, although I haven't looked closer into that solution.

这可能是一项很大的努力,但是如果一个设计良好的解决方案就是您所追求的,我认为您最好的选择是创建一个扩展服务处理程序的类,它实现您自己的自定义序列化程序,您可以在其中检查对于maxJsonLength。然后,您可以设置web.config以让所有(或某些)asmx请求由您自己的处理程序处理。 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. 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; 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).

另一种解决方案,可能更容易实现,并且仍然可以说是非常简洁,可以用你自己的函数覆盖(或包装)jQuery的$ .ajax函数,在客户端上对JSON进行字符串化(无论如何,这样做,首先创建请求,因此它不应该是很多开销),并在那里检查长度。使用这种方法的好处是你不必乱用扩展服务处理程序,而且,截断对象的部分会更容易,因为你的javascript对象在手边可读;你可以迭代它,字符串化它的子集,检查它们的长度,或者使用你喜欢的wahtever机制来实现智能截断(显然无论你想要使用什么方法截断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,然后用自己的函数覆盖$ .ajax,调用$ .ajaxCore。

#2


2  

Maybe How to: Handle Page-Level Errors will help.

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

#3


2  

You can also try to add a global exception handler in your app .

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

#4


1  

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 (e.g. 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设置为限制的两倍。

#5


0  

If you can't access the underlying code of the serializer which actually throws the exception to fix it, maybe custom error handlers are an option?

如果您无法访问实际抛出异常来修复它的序列化程序的底层代码,那么自定义错误处理程序可能是一个选项吗?