Multiple methods for using Javascript to call Webservice

Source: Internet
Author: User

Copy codeThe Code is as follows:
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;
}
}

It's still vulgar. :)
2. js calls the implementation part of webservice + xmlhttp.
Copy codeThe Code is as follows:
<Html>
<Title> Call webservice with javascript and xmlhttp. </title>
<Body>
<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"?> ';
Datadata = 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/"> ';
Datadata = data + '<soap: Body> ';
Datadata = data + '<SayHelloTo xmlns = "http://tempuri.org/"> ';
Datadata = data + '<Name>' + value + '</Name> ';
Datadata = data + '</SayHelloTo> ';
Datadata = data + '</soap: Body> ';
Datadata = 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 );
}


// --> </Script>

<Input type = "button" value = "CallWebserviceByGet" onClick = "RequestByGet (null)">
<Input type = "button" value = "CallWebserviceByPost" onClick = "RequestByPost ('zach ')">
</Body>
</Html>

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)
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
}
}
<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)">
Attached to the ibm article on ajax webservice call:
Use Ajax to call the SOAP Web service. Part 1: Construct a Web service client

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.