WebMethod vs ScriptMethod

I have an aspx.NET 3.5 place with a method marked with the [WebMethod] attribute. I call this using jQuery, sending JSON in both directions. It all works great. My question is: what does [ScriptMethod] when applied to a method? I tried this and it seems to give the same result. Are ScriptMethod and WebMethod identical and interchangeable, or do they provide functionality and / or overhead that the other doesn't have? In general, I find myself confused with all the options available for implementing web services, and I would like to know what the pros and cons are for everyone.

+41
web-services pagemethods
Jun 02 '09 at 19:36
source share
2 answers

You use the ScriptMethod attribute in the following two scripts.

  • You are using jquery or any other ajax request mechanism, but you want the request to be GET, not POST.
  • You want to receive a processed XML response in javaScript.

If you do not have one of the above requirements; you just need a JSON response using an ajax request, then you can just use WebMethod.

Is there another confusing element here when you use the ScriptService attribute? this is used if you are using the Microsoft Ajax Client script infrastructure, these attributes tell the server to generate proxy objects on the client so that you can call functions just like a regular object. var MyRemoteObject = new RemoteObject(); MyRemoteObject.getMessage(....) , and even if you use the ScriptService attribute, you do not need to add the ScriptMethod attribute only in the above scripts.

This baffled me at the beginning, because I thought the ScriptService and ScriptMethod attributes work together the same as the WebService and WebMethod attributes.

+29
Feb 11 '10 at 18:55
source share

The ScriptMethodAttribute attribute is optional. (However, methods that can be called from the client script must have the System.Web.Services .. ::. WebMethodAttribute attribute.). If the method is not marked with ScriptMethodAttribute, the method will be called using the HTTP POST command, and the response will be serialized as JSON. You cannot override this parameter from a script.

from - http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx

EDIT: WebMethod and ScriptMethod are not competing attributes. ScriptMethod may be an additional annotation, as indicated in the paragraph above.

+2
Jun 02 '09 at 19:46
source share



All Articles