First, we should first add a WEB Service (WebService. asmx) to the project, and write the code in the code file (WebService. cs) as follows:
WebService. csusing System;
Using System. Collections;
Using System. Web;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Web. Script. Services;
/// <Summary>
/// Summary of WebService
/// </Summary>
///
[ScriptService]
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
Public class WebService: System. Web. Services. WebService
{
Public WebService ()
{
// If you use the designed component, uncomment the following line
// InitializeComponent ();
}
[WebMethod]
Public string HelloWorld ()
{
Return "Hello World ";
}
[WebMethod] //
Public int ADD (int a, int B)
{
Return a + B;
}
}
The most important thing is the 12th rows. After adding this to [ScriptService], you can use js to call the WEB
Services. Note that "[ScriptService]" requires a namespace. net3.5 is System. web. script. services; this space was not found in section 2.0. I hope you can help me explain it. The following describes how to call this web service through js.
Default. aspx <script language = "javascript">
Function OnbuttonGo_click ()
{
RequestSimpleService = WebService. HelloWorld (
// Params
OnRequestComplete // Complete event
);
Return false;
}
Function OnRequestComplete (result)
{
Alert (result );
}
</Script>
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Services>
<Asp: ServiceReference Path = "WebService. asmx"/>
</Services>
</Asp: ScriptManager>
<Input type = "button" value = "Call webservice" onclick = "return OnbuttonGo_click ();"/> to add a ScriptManager to the html part, add the ServiceReference reference to the ScriptManager, otherwise, an error is reported. When you add a client button to call the click event, you can directly use the class name and method provided by WEBService in event processing. A function completed by the request should be added at the end of the parameter list of the method, to return results after processing is complete. Similarly, the add method in WebService can also be called like this. You can add the input parameters to the params part.
This article is a series of articles about ASP. net ajax from TerryLee.
Http://www.cnblogs.com/Terrylee/archive/2006/10/25/ASPNET_AJAX_ScriptManager.html
An error has been updated. The following is an example of downloading. This time it is available!
Download demo