Ajax. Net call WebService Method

Source: Internet
Author: User

Ajax. as one of the mature Ajax platforms. NET platform is developed by Microsoft, so Ajax is used. net client script call. net Development of WebService will be a very simple thing, this is one of the exciting thing.

Next, we will introduce the call process step by step. First, we first use vs2008 to add a WebService, itsCodeAs follows:

  1. UsingSystem;
  2. UsingSystem. collections;
  3. UsingSystem. componentmodel;
  4. UsingSystem. Data;
  5. UsingSystem. LINQ;
  6. UsingSystem. Web;
  7. UsingSystem. Web. Services;
  8. UsingSystem. Web. Services. Protocols;
  9. UsingSystem. xml. LINQ;
  10. NamespaceWebservice1
  11. {
  12. /// <Summary>
  13. /// Summary of service1
  14. /// </Summary>
  15. [WebService (namespace ="Http://tempuri.org /")]
  16. [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
  17. [Toolboxitem (False)]
  18. // To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
  19. [System. Web. Script. Services. scriptservice]
  20. Public ClassService1: system. Web. Services. WebService
  21. {
  22. [Webmethod]
  23. Public StringHelloworld ()
  24. {
  25. Return "Hello World";
  26. }
  27. }
  28. }

Note: add the attribute [system. Web. Script. Services. scriptservice] To the WebService class so that ASP. Net AJAX can call this web service from the script.

Add an Ajax web page and add a reference to WebService to the page. You can write script code to call this WebService. The Code is as follows:

  1. <% @ PageLanguage="C #" Autoeventwireup="True" Codebehind="Webform1.aspx. cs" Inherits="Webservice1.webform1"%>
  2. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <Html Xmlns=Http://www.w3.org/1999/xhtml" >
  4. <Head Runat="Server">
  5. <Title>No title page</Title>
  6. <Script Type="Text/JavaScript">
  7. Function callmethod ()
  8. {
  9. Webservice1.service1. helloworld (succeededcallback );
  10. }
  11. Function succeededcallback (RES)
  12. {
  13. Alert (RES );
  14. }
  15. </Script>
  16. </Head>
  17. <Body>
  18. <Form ID="Form1" Runat="Server">
  19. <Div>
  20. <ASP: scriptmanager ID="Scriptmanager1" Runat="Server">
  21. <Services>
  22. <ASP: servicereference Path="~ /Service1.asmx" />
  23. </Services>
  24. </ASP: scriptmanager>
  25. </Div>
  26. <Input ID="Button2" Type="Button" Value="WebService method call" Onclick="Callmethod ()" />
  27. </Form>
  28. </Body>
  29. </Html>

Call methods are almost the same as function calls in C #. They can be called in the form of namespaces, type names, method names, and parameters. in addition, we only need to add a callback method to process the returned results of the call. for basic parameters, you can directly pass them, complex parameters I use the popular JSON technology as the carrier for data transmission. For details about JSON technology, refer to the following article.Article:

JSON Technology and Its Application in JBF for ASP. NET

In addition, the BS part of the ultimate basic business platform is the use of this total JS + webserver technology, a realistic and friendly user interface, you can download from the following URL to the free trial version of the platform.

Http://www.jeez.com.cn/jbf/

Related Article

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.