[C-sharp]View plaincopyprint?
- Using System;
- Using System. Web;
- Using System. Web. Services;
- Using System. Web. Services. Protocols;
- [Webservice (namespace = "http://tempuri.org/")]
- [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
- Public class Service: System. Web. Services. WebService
- {
- Public Service ()
- {
- // Uncomment the following line if using designed components
- // InitializeComponent ();
- }
- [Webmethod]
- Public string SayHelloTo (string Name)
- {
- Return "Hello" + Name;
- }
- }
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?
- <Html>
- <Title> Call webservice with javascript and xmlhttp. </title>
- <Body>
- <Mce: script language = "javascript"> <! --
- // Test function with get method.
- Function RequestByGet (data ){
- Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
- // Webservice location.
- Var URL = "http: // localhost: 1323/WebSite6/Service. asmx/SayHelloTo? Name = Zach ";
- Xmlhttp. Open ("GET", URL, false );
- Xmlhttp. SetRequestHeader ("Content-Type", "text/xml; charset = UTF-8 ");
- Xmlhttp. SetRequestHeader ("SOAPAction", "http://tempuri.org/SayHelloTo ");
- Xmlhttp. Send (data );
- Var result = xmlhttp. status;
- // OK
- If (result = 200 ){
- Document. write (xmlhttp. responseText );
- }
- Xmlhttp = null;
- }
- // Test function with post method
- Function RequestByPost (value)
- {
- Var data;
- Data = '<? Xml version = "1.0" encoding = "UTF-8"?> ';
- 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/"> ';
- Data = data + '<soap: Body> ';
- Data = data + '<safety helloto xmlns = "http://tempuri.org/"> ';
- Data = data + '<Name>' + value + '</Name> ';
- Data = data + '</SayHelloTo> ';
- Data = data + '</soap: Body> ';
- Data = data + '</soap: Envelope> ';
- Var xmlhttp = new ActiveXObject ("Microsoft. XMLHTTP ");
- Var URL = "http: // localhost: 1323/WebSite6/Service. asmx ";
- Xmlhttp. Open ("POST", URL, false );
- Xmlhttp. SetRequestHeader ("Content-Type", "text/xml; charset = gb2312 ");
- Xmlhttp. SetRequestHeader ("SOAPAction", "http://tempuri.org/SayHelloTo ");
- Xmlhttp. Send (data );
- Document. write (xmlhttp. responseText );
- }
- // --> </Mce: script>
- <Input type = "button" value = "CallWebserviceByGet" onClick = "RequestByGet (null)">
- <Input type = "button" value = "CallWebserviceByPost" onClick = "RequestByPost ('zach ')">
- </Body>
- </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?
- Function getfemale ()
- {
- // The first parameter is the url of webservice, followed by the name
- Female. useService ("news. asmx? WSDL "," news ");
- // 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.
- IntCallID = female. news. callService (female_result, "getphoto", "female"); // There are two parameters .....
- }
- Function female_result (result) // callback function
- {
- If (result. error)
- {
- Female. innerHTML = result. errorDetail. string;
- }
- Else
- {
- Female. innerHTML = result. value; // write the results returned by webservice as in div
- }
- }
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)">