How do I call the methods in webservice directly from the client?
1.web.config needs to be configured to run the ASP.NET AJAX framework corresponding configuration items, of course, to build a asp.net ajax-Enabled Web site project, Web.config has been configured.
2. Want to let a certain webservice can be called JS, need to do a few steps:
I. In this webservice file, use the "Using System.Web.Script.Services;" Introduces this namespace.
Ii. Add the "[ScriptService]" attribute above this class.
Iii. add the [WebMethod] property to the method that needs to be called.
Specific examples can refer to the ASP.net AJAX client programming trip (i)--hello! asp.net AJAX "SayHelloService.cs code.
3. The client page that calls WebService should also be prepared accordingly. The first is to have a ScriptManager control in the page, and then you need to indicate the location of the WebService file. Such as:
<Services>
<asp:ServiceReference Path="~/SayHelloService.asmx" />
</Services>
Put this code between <asp:ScriptManager> and <asp:scriptmanager/> tags, you can make this page JS can directly call the Sayhelloservice.asmx in the method. Specific code can refer to the "ASP.net Ajax client Programming Journey" (i)--hello! asp.net AJAX in the second example of the Default.aspx code.
To do the above three preparation, the JS code on this page can use our familiar "class name. Method Name" method directly call the method in WebService.
The story behind the Magic
We know that although we always say "direct call", this refers to "direct invocation" at the grammatical level. In fact, JavaScript code can not really call the daemon directly, in the middle of the XMLHttpRequest object must be used. So who was it that gave us the "camouflage"? The answer is in the following picture:
The image above is the real secret of this so-called "direct call". As pictured, now suppose there is a WebService class, called Class1, with two methods: Method1 and METHOD2. When this webservice is processed by the method we mentioned above, the ASP.net Ajax framework automatically analyzes the class and generates a client proxy (proxy) for this class on the client, which is a JavaScript object whose class name, Method names are consistent with background classes. And when we call from the client's JS script, we actually call this proxy class, and the proxy class uses the XMLHttpRequest object, invoking the background class through the traditional Ajax way. In this way, we feel as if we were directly calling the background similar in the foreground.
But...... Where it still seems to be different ...
Through the above analysis, we know that asp.net ajax call back-end method, after all, the use of XMLHttpRequest objects, so that the limitations of traditional Ajax, so that we can not really like in the background to invoke the method as convenient.
For example, we use "variable = class name" in the background. Method name or variable = object name. Method name, so that the variable directly gets the return value of the method, but not Ajax, it must get the return value through the callback function, which is also reflected in the use of ASP.net ajax. As you can see from an example of an article, even with the ASP.net Ajax framework, you still need to use a callback function, but it's syntactically simplified.
The syntax for calling the background class is formally given below:
Class name. Method name (parameter 1, Parameter 2, ..., parameter n, callback function)
That is, there are two differences on the call: one does not need to use a variable to receive the return value, and the other is to add a parameter after all the arguments: the callback function. For example, there is a method of a class that is called in the background:
var1 = Class1.method1 (PARA1,PARA2);
When you switch to the foreground JS call, it looks like this:
CLASS1.METHOD1 (para1,para2,callbackfunction);