Getting the best error message from ASP.Net [WebMethod] called from jQuery

[NOTE: I'm really looking for good debugging methods here. Perhaps some tricks or ways to simplify things that I don't know about.]

I use the [WebMethods] call method defined on the ASPX page from jQuery, as mentioned here and here . This seems to be an increasingly common method.

I used it for a while, and overall it works great. But in development, it is rather fragile. Any incorrect parameter will result in a truly indefinite, non-specific error message. For example, if I have a rather sophisticated web method defined as:

[WebMethod] public static string SaveComplexRecord(int recID, GeneralData general, SomeObject data, SomeOtherObject moreData) { //do a bunch of stuff with that data } 

And in GeneralData, SomeObject and SomeOtherObject there is a combination of different types of parameters (strings, ints, bools, datetimes.) It is very likely, especially during the initial development, that I will incorrectly create JSON on the client side, Maybe I will do this:

 var data = { recID: curID, general: { a: aValue, b: bValue, c: cValue }, data: { d: dValue, e: eValue, f: fValue }, moredata: { g: gValue, h: hValue, i: iValue } }; 

This will result in an error, because the name of the third parameter is more than Data, not moredata. And this is just an example, maybe any of one hundred other subtle mistakes in the style of typos.

If I called this method from C #, the compiler would give me an error message like "No overloaded method of SaveComplexRecord takes three parameters." or some other useful message pointing you in the right direction.

So ... is there a way to get ASP.Net to create better error messages here?

Or is there any utility that will automatically build a JSON parameter structure for calling [WebMethod]? (just like you can automatically get WSDL web services)

... or any other method I might lose?

And for completeness, here I call these WebMethods from jQuery:

  var jsondata = $.toJSON(data); $.ajax({ type: "POST", url: "MyWebPage.aspx/SaveComplexRecord", data: jsondata, contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: function(xhr) { xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); }, success: function(msg) { //do something on success }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("ERROR status:" + textStatus + " error:" + errorThrown); } }); 
+4
source share
4 answers

Or is there any utility that will automatically build a JSON parameter structure for calling [WebMethod]? (just like you can automatically get WSDL web services)

Yes! ASP.Net AJAX framework can do it! You can get the structure for creating client-side proxy classes for the GeneralData , SomeObject and SomeOtherObject using the GenerateScriptType attribute in the web service class.
See an understanding of asp net ajax web services for a very good article on the subject.

[Unfortunately, AFAIAA, GenerateScriptType has no effect when applied to the page class where your page method is defined, so you will need to add .asmx to get proxy generation.]

Perhaps you could use these classes to create a data structure that JSON then builds when calling .ajax ? [One of the (very few) things that I really like about the MS AJAX framework is client-side proxy generation: it really makes it easy to call web services and page methods. Having said that, I also move on to using jQuery in MS AJAX preferences.]

Or alternatively ... Your problem is that the de-serialization of JSON data in the arguments of your page method is carried out transparently using the framework (which is good in most cases), but when everything goes wrong, the feedback you receive is less than -helpful. If you want to catch the problems of de-serialization, I think you need to take control of serialization either using custom JSON converters (see here ) or using a rather inelegant approach to the sledgehammer, when your method takes a string and serializes JSN on its own in the method - which is trivial with any of the many JSON libraries out there.

+1
source

Javascript is dynamically typed, so you cannot get a compile-time error. But you can use the old window.onerror + ajax trick (or send an error via ajax in the error jQuery.ajax() )), and as soon as you find yourself on the server, you can treat it the same way as any other time error execution (throw an exception, write down the error, whatever)

0
source

From a jQuery point of view, your problem is to declare a function error. Take only one input parameter and it will have all the error properties, then you can easily debug it.

If the problem is with the server, catch the error there and create a return json containing the error message.

Oh, and if you want to test your javascript at compile time, I recommend the add-on from jslint.com.

So:

$. Ajax ({type: "POST", url: "MyWebPage.aspx / SaveComplexRecord", data: jsondata, contentType: "application / json; charset = utf-8", dataType: "json", beforeSend: function (xhr) { xhr.setRequestHeader ("Content-Type", "application / json; charset = utf-8");}, success: function (msg) {// do something with success}, error: function (err) {notifications (e.message);}});

0
source

What I do when returning JSON from the web service has an object called ret containing the err attribute, as well as a data attribute containing the result of the service call. Inside the web service, I catch all exceptions and put the exception message in the "err" attribute. Then, in the client, I verify that the err attribute is not empty if it knows that an error has occurred.

0
source

All Articles