Javascript + XMLHTTP call WebService

Source: Internet
Author: User

1.Create a WebService. To avoid vulgarity, I slightly modified the default webmethod for creating webserice. Pai_^
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.

<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)
{< br> var data;
DATA = ' ';
DATA = Data +' ';
DATA = Data + ' ';
DATA = Data + ' ';
DATA = Data + ' ' + value + ' ';
DATA = Data + ' ';
DATA = Data + ' ';
DATA = Data + ' ';

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.

I found that the POST method has a slow response because of the large amount of data sent in the POST method?

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.