简体   繁体   中英

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/

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.

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?

I have set a try/catch block within my method to no avail, it is happening outside my method (during JSON Serialization).

StackTrace looks like this:

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.

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.

Be aware, though, that manually truncating a JSON string, without breaking the syntax, could be quite an effort in itself.

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).

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 .

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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