Multiple methods for Javascript to call Webservice.

Source: Internet
Author: User
[C-sharp]View plaincopyprint?
  1. Using System;
  2. Using System. Web;
  3. Using System. Web. Services;
  4. Using System. Web. Services. Protocols;
  5. [Webservice (namespace = "http://tempuri.org/")]
  6. [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
  7. Public class Service: System. Web. Services. WebService
  8. {
  9. Public Service ()
  10. {
  11. // Uncomment the following line if using designed components
  12. // InitializeComponent ();
  13. }
  14. [Webmethod]
  15. Public string SayHelloTo (string Name)
  16. {
  17. Return "Hello" + Name;
  18. }
  19. }

Using System; <br/> using System. web; <br/> using System. web. services; <br/> using System. web. services. protocols; </p> <p> [webservice (namespace = "http://tempuri.org/")] <br/> [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] <br/> public class Service: System. web. services. webService <br/> {<br/> public Service () <br/>{< br/> // uncomment the following line if using designed components <br/> // InitializeComponent (); <br/>}</p> <p> [webmethod] <br/> public string SayHelloTo (string Name) <br/>{< br/> return "Hello" + Name; <br/>}< br/> 

 

It's still vulgar. :)

2. js calls the implementation part of webservice + xmlhttp.

[Xhtml]View plaincopyprint?
  1. <Html>
  2. <Title> Call webservice with javascript and xmlhttp. </title>
  3. <Body>
  4. <Mce: script language = "javascript"> <! --
  5. // Test function with get method.
  6. Function RequestByGet (data ){
  7. Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
  8. // Webservice location.
  9. Var URL = "http: // localhost: 1323/WebSite6/Service. asmx/SayHelloTo? Name = Zach ";
  10. Xmlhttp. Open ("GET", URL, false );
  11. Xmlhttp. SetRequestHeader ("Content-Type", "text/xml; charset = UTF-8 ");
  12. Xmlhttp. SetRequestHeader ("SOAPAction", "http://tempuri.org/SayHelloTo ");
  13. Xmlhttp. Send (data );
  14. Var result = xmlhttp. status;
  15. // OK
  16. If (result = 200 ){
  17. Document. write (xmlhttp. responseText );
  18. }
  19. Xmlhttp = null;
  20. }
  21. // Test function with post method
  22. Function RequestByPost (value)
  23. {
  24. Var data;
  25. Data = '<? Xml version = "1.0" encoding = "UTF-8"?> ';
  26. Data = data + '<soap: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: soap = "http://schemas.xmlsoap.org/soap/envelope/"> ';
  27. Data = data + '<soap: Body> ';
  28. Data = data + '<safety helloto xmlns = "http://tempuri.org/"> ';
  29. Data = data + '<Name>' + value + '</Name> ';
  30. Data = data + '</SayHelloTo> ';
  31. Data = data + '</soap: Body> ';
  32. Data = data + '</soap: Envelope> ';
  33. Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
  34. Var URL = "http: // localhost: 1323/WebSite6/Service. asmx ";
  35. Xmlhttp. Open ("POST", URL, false );
  36. Xmlhttp. SetRequestHeader ("Content-Type", "text/xml; charset = gb2312 ");
  37. Xmlhttp. SetRequestHeader ("SOAPAction", "http://tempuri.org/SayHelloTo ");
  38. Xmlhttp. Send (data );
  39. Document. write (xmlhttp. responseText );
  40. }
  41. // --> </Mce: script>
  42. <Input type = "button" value = "CallWebserviceByGet" onClick = "RequestByGet (null)">
  43. <Input type = "button" value = "CallWebserviceByPost" onClick = "RequestByPost ('zach ')">
  44. </Body>
  45. </Html>

<Html> <br/> <title> Call webservice with javascript and xmlhttp. </title> <br/> <body> <br/> <mce: script language = "javascript"> <! -- </P> <p> // test function with get method. <br/> function RequestByGet (data) {<br/> var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP "); <br/> // Webservice location. <br/> var URL = "http: // localhost: 1323/WebSite6/Service. asmx/SayHelloTo? Name = Zach "; <br/> xmlhttp. open ("GET", URL, false); <br/> xmlhttp. setRequestHeader ("Content-Type", "text/xml; charset = UTF-8"); <br/> xmlhttp. setRequestHeader ("SOAPAction", "http://tempuri.org/SayHelloTo"); <br/> xmlhttp. send (data); <br/> var result = xmlhttp. status; <br/> // OK <br/> if (result = 200) {<br/> document. write (xmlhttp. responseText); <br/>}< br/> xmlhttp = null; <br/>}</p> <p> // test function with po St method <br/> function RequestByPost (value) <br/>{< br/> var data; <br/> data = '<? Xml version = "1.0" encoding = "UTF-8"?> '; <Br/> data = data +' <soap: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: soap = "http://schemas.xmlsoap.org/soap/envelope/"> '; <br/> data = data + '<soap: Body>'; <br/> data = data + '<SayHelloTo xmlns = "http://tempuri.org/"> '; <br/> data = data + '<Name>' + value + '</Name>'; <br/> data = data + '</SayHelloTo> '; <br/> data = data + '</soap: Body>'; <br/> data = data + '</soap: Envelope> '; </p> <p> var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP "); <br/> var URL =" http: // localhost: 1323/WebSite6/Service. asmx "; <br/> xmlhttp. open ("POST", URL, false); <br/> xmlhttp. setRequestHeader ("Content-Type", "text/xml; charset = gb2312"); <br/> xmlhttp. setRequestHeader ("SOAPAction", "http://tempuri.org/SayHelloTo"); <br/> xmlhttp. send (data); <br/> document. write (xmlhttp. responseText); <br/>}</p> <p> // --> </mce: script> </p> <input type = "button" value = "CallWebserviceByGet" onClick = "RequestByGet (null) "> <br/> <input type =" button "value =" CallWebserviceByPost "onClick =" RequestByPost ('zach ') "> <br/> </body> <br/> </ptml>

 

You can find the heap Dongdong that needs to be sent using the post method on the webservice test page, and add the corresponding parameters together.

Method implemented through style. behavior (relatively simple)

Address: http://www.zahui.com/html/4/37953.htm

 

[Javascript]View plaincopyprint?
  1. Function getfemale ()
  2. {
  3. // The first parameter is the url of webservice, followed by the name
  4. Female. useService ("news. asmx? WSDL "," news ");
  5. // Set a callback function, which calls back when the service returns the result. The first parameter is the name of the callback function, followed by the webservice parameter.
  6. IntCallID = female. news. callService (female_result, "getphoto", "female"); // There are two parameters .....
  7. }
  8. Function female_result (result) // callback function
  9. {
  10. If (result. error)
  11. {
  12. Female. innerHTML = result. errorDetail. string;
  13. }
  14. Else
  15. {
  16. Female. innerHTML = result. value; // write the results returned by webservice as in div
  17. }
  18. }

Function getfemale () <br/>{< br/> // The first parameter is the url of webservice, followed by the name <br/> female. useService ("news. asmx? WSDL "," news "); <br/> // sets a callback function that calls back when the service returns the result. The first parameter is the name of the callback function, the following is the webservice parameter <br/> intCallID = female. news. callService (female_result, "getphoto", "female"); // There are two parameters ..... <br/>}</p> <p> function female_result (result) // callback function <br/>{< br/> if (result. error) <br/>{< br/> female. innerHTML = result. errorDetail. string; <br/>}< br/> else <br/>{< br/> female. innerHTML = result. value; // write the results returned by webservice as in div <br/>}< br/>}
<Div id = "female" style = "BEHAVIOR: url (WebService. htc)"> </div>

 

OK. This provides a way to call dynamic content on a static page;
If you add a regular call to the getfemale () function, the page is refreshed.
The disadvantage is that the webservice has a certain latency. Even the local webservice will be much slower than the static page, and the first time you open the page, it will feel very uncoordinated.

 

The second method uses style. The code is much simpler, and css is used to define the behavior of div. It is much easier to read than the first method :)

Style = "behavior: url (webservice. htc )"

Prerequisites:

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. the "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP ).

Note: Another summary post in: http://goody9807.cnblogs.com/archive/2005/08/17/216725.html

 

Calling WebServices using Javascript

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. the "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP ).

To use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:

 

Style = "behavior: url (webservice. htc)">

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.