The common function of JS calling WebService

Source: Internet
Author: User
Tags http post soap xmlns

Call:

Requestbypost (Method,variable,value,url,_namespace)

Method:webservice's method name

Variable:webservice the variable array group of the method

An array of the values of the variables of the Value:webservice method

URL: The requested address (ASMX file address)

The _namespace:webservice namespace

<script language= "javascript" type= "Text/javascript" >
<! cdata[

Define
var xmlhttp;
var value=new Array ();
var variable=new Array ();

Show Response MSG.
function Handlestatechange ()
{
var H=document.getelementbyid ("Label1");
if (xmlhttp.readystate==4)
{
if (xmlhttp.status==200)
{
alert (Xmlhttp.responsetext);
H.innerhtml=xmlhttp.responsetext;
H.innerhtml=xmlhttp.responsexml;
}
else if (xmlhttp.status==404)
{
H.innerhtml= "<br> cannot find the requested server resource! ";
}
}
else if (xmlhttp.readystate==0)
{
H.innerhtml= "<br> not initialized! ";
}
else if (xmlhttp.readystate==1)
{
H.innerhtml= "<br> Loading ...! ";
}
else if (xmlhttp.readystate==2)
{
H.innerhtml= "<br> has been loaded complete! ";
}
else if (xmlhttp.readystate==3)
{
H.innerhtml= "<br> is interacting with the server";
}
Else
{
H.innerhtml=xmlhttp.responsexml;
}

}

Get Request Data ' s length
function Getlen (str)
{
var bytescount=0;
for (var i = 0; i < str.length; i++)
{
var c = Str.charat (i);
if (/^[\u0000-\u00ff]$/.test (c))//Match Double Byte
{
Bytescount + 1;
}
Else
{
Bytescount + 2;
}
}
return bytescount;

}

Create XMLHttpRequest Object
function Createxmlhttprequest ()
{

if (window. ActiveXObject)
{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
else if (window. Xmlhttprequst)
{
Xmlhttp=new XMLHttpRequest ();
}
}

Send Request by HTTP POST

function Requestbypost (method,variable,value,url,_namespace)
{
Createxmlhttprequest ();
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 + ' < ' +method+ ' xmlns= ' ' +_namespace+ ' ' > ';
for (Var i=0;i<variable.length;i++)
{
data = data + ' < ' +variable[i]+ ' > ' +value[i]+ ' </' +variable[i]+ ' > ';

}
data = data + ' </' +method+ ' > ';
data = data + ' </soap:Body> ';
data = data + ' </soap:Envelope> ';

Xmlhttp.onreadystatechange=handlestatechange;
xmlHTTP. Open ("POST", url, True);
xmlHTTP. setRequestHeader ("Content-type", "Text/xml; Charset=utf-8");
xmlHTTP. setRequestHeader ("Content-length", Getlen (data));
xmlHTTP. setRequestHeader ("SOAPAction", _namespace+method);
xmlHTTP. Send (data);
alert (data);
}

callhelloworld!
function Sayhello_onclick () {
Alert (document.getElementById (' YourName '). Value);
Requestbypost ("HelloWorld", New Array ("MSG"), new Array (document.getElementById (' YourName '). Value), " Webservice.asmx "," localhost/");
}
Weatherreport Test:
function Button2_onclick () {
Requestbypost ("Getweatherbycityname", New Array ("Thecityname"), new Array (document.getElementById (' CityName '). Value), "Http://www.webxml.com.cn/WebServices/WeatherWebService.asmx", "http://WebXml.com.cn/");
}

]]>
</script>
////////////////////////////////
Sixi. Let it be.../////
//////////////////////////////

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.