Ajax asynchronously calls WebService

Source: Internet
Author: User

 

Code

1 <HTML xmlns = "http://www.w3.org/1999/xhtml">
2 3 <title> </title>
4 <script language = "JavaScript" type = "text/JavaScript">
5 function ajaxcall (){
6 var XMLHTTP;
7 try {// Firefox, opera 8.0 +, Safari
8 XMLHTTP = new XMLHttpRequest (); // instantiate the XMLHTTPRequest object
9}
10 catch (e ){
11
12 // Internet Explorer 5 +
13 try {
14 XMLHTTP = new activexobject ("msxml2.xmlhttp ");
15}
16 catch (e ){
17
18 try {
19 XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
20}
21 catch (e ){
22 alert ("the browser does not support Ajax! ");
23 return false;
24}
25}
26}
27 // bind the data processing function.
28 XMLHTTP. onreadystatechange = function (){
29 If (XMLHTTP. readystate = 4 ){
30 if (XMLHTTP. Status = 200 ){
31 document. getelementbyid ('lbtext'). innerhtml = XMLHTTP. responsetext;
32}
33 else {
34 alert ('request error .');
35}
36
37
38}
39}
40 XMLHTTP. Open ("Post", "http: // localhost: 1576/webservice1.asmx/helloworld", true); // asynchronous request data
41 XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded;"); // for post requests, set the request requestheader. Otherwise, XMLHTTP. Status is 50042.
43 XMLHTTP. Send ("strname = GG"); // The strname is the number of parameters in the helloworld WebService method.
44
45}
46
47 </SCRIPT>
48
49
50 51 <body>
52 <Form ID = "form1" runat = "server">
53 <div>
54 <asp: Label id = "lbtext" runat = "server"> </ASP: Label>
55 <a href = "javascript: void (0)" onclick = 'ajaxcall () '> click here </a>
56 </div>
57 </form>
58 </body>
59 60
61
62
63 ======= WebService ==========
64
65 [WebService (namespace = "http://tempuri.org/")]
66 [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
67 [system. componentmodel. toolboxitem (false)]
68 // to allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
69 // [system. Web. Script. Services. scriptservice]
70 public class webservice1: system. Web. Services. WebService
71 {
72
73 [webmethod, scriptmethod] // set scriptmethod to the WebService method called by the client
74
75 Public String helloworld (string strname)
76 {
77 return strname + "Hello World ";
78}
79}
80
81

 

 

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.