JS Call WebService two ways

Source: Internet
Author: User
Tags soap

The protocol is definitely using the HTTP protocol, because the SOAP protocol itself is also based on the HTTP protocol. Interim second way: Only webservice3.5 later version can be successful

The first way: to construct the body of the SOAP format, pay attention to the bold yellow logo, for example:

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:Header>"
data = Data + ' <usersoapheader xmlns= ' http://tempuri.org/' > '
data = data + "<UserName>admin</UserName>"
data = data + "<Pwd>faaaa</Pwd>"
data = data + "</UserSoapHeader>"
data = data + "</soap:Header>"


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);

The second way (I didn't use it, I thought it wouldn't work): Constructs a JSON-formatted body, such as:

Createxmlhttprequest ();

URL : Http://....../a.asmx/webservice Method Name

Body using JSON data format

XMLHTTP. setRequestHeader ("Content-type", "application/json; Charset=utf-8");
XMLHTTP. setRequestHeader ("Content-length", Getlen (body));

JS Call WebService two ways

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.