首页 > 代码库 > JS调用webservice的两种方式
JS调用webservice的两种方式
协议肯定是使用http协议,因为soap协议本身也是基于http协议。期中第二种方式:只有webservice3.5以后版本才可以成功
第一种方式:构造soap格式的body,注意加粗的黄色标识,比如:
createXMLHttpRequest();
var data;
data = http://www.mamicode.com/‘<?xml version="1.0"encoding="utf-8"?>‘;
data = http://www.mamicode.com/data + ‘
data = http://www.mamicode.com/data + ‘
data = http://www.mamicode.com/data +"<Pwd>faaaa</Pwd>"
data = http://www.mamicode.com/data +"</UserSoapHeader>"
data = http://www.mamicode.com/data +"</soap:Header>"
data = http://www.mamicode.com/data + ‘
data = http://www.mamicode.com/data + ‘<‘+method+‘"‘+_Namespace+‘">‘;
for(var i=0;i<variable.length;i++)
{
data = http://www.mamicode.com/data + ‘<‘+variable[i]+‘>‘+value[i]+‘‘+variable[i]+‘>‘;
}
data = http://www.mamicode.com/data + ‘‘+method+‘>‘;
data = http://www.mamicode.com/data + ‘
data = http://www.mamicode.com/data + ‘‘;
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);
第二种方式(我没使用过,想着不会成功):构造json格式的body,比如:
createXMLHttpRequest();
url:http://....../a.asmx/webservice方法名
body使用json数据格式
xmlhttp.SetRequestHeader("Content-Type","application/json; charset=utf-8");
xmlhttp.SetRequestHeader("Content-Length",getlen(body));
JS调用webservice的两种方式