I'm using jQuery .Ajax to call a method from my codebehind which must be a public static method. Because of this, I can't access any of my pages controls unless they are declared also as public static and I don't want any of my variables or anything declared that way. If I return a List in a WebMethod, what can I do in the catch to have an error bubble back up to the client side page? The only way I can see being able to do this the way I want is if the WebMethod returns a string instead of a List.
1 Answer
Why not just throw an exception? Here is a good article on how to catch and parse WebMethod exceptions using jQuery.
2 Comments
Shane LeBlanc
Thing is I was trying to wrap my code in my WebMethod in a try catch and wasn't able to return ex.Message in the catch because it was a string and the WebMethod returns a List<T>. I guess I don't need to do a try/catch at all after reading that article. Thanks for the link :)
mgnoonan
Don't try to shoe-horn everything into one response type. Throwing exceptions across a boundry is a perfectly acceptable way of communicating an error. :)