WebMethod vs ScriptMethod

前端 未结 2 1876
陌清茗
陌清茗 2020-12-04 18:01

I have a .NET 3.5 aspx place with a method marked with the [WebMethod] attribute. I\'m calling this with jQuery, sending JSON in both directions. This all works

2条回答
  •  情书的邮戳
    2020-12-04 18:21

    You use the ScriptMethod attribute in the following 2 scenarios.

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

    If you don't have one of the above requirements; you just need a JSON response using a an ajax request then you can simply use the WebMethod.

    There is still one more confusing element here, when do you use the ScriptService attribute? this is used if you are using the Microsoft Ajax Client script framework, this attributes tell the server to generate proxy objects on the client so that you can call functions just like a normal object. var MyRemoteObject = new RemoteObject(); MyRemoteObject.getMessage(....) and even when you use the ScriptService attribute you don't need to add the ScriptMethod attribute only in the above scenarios.

    It was confusing to me at the begining because i thought that the ScriptService and the ScriptMethod attributes works together just like the WebService and WebMethod attributes.

提交回复
热议问题