Simple implementation of WebService calling using js

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.