syf

JS调用webservice的两种方式

协议肯定是使用http协议,因为soap协议本身也是基于http协议。期中第二种方式:只有webservice3.5以后版本才可以成功

 

第一种方式:构造soap格式的body,注意加粗的黄色标识,比如:

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

第二种方式(我没使用过,想着不会成功):构造json格式的body,比如:

createXMLHttpRequest();  

urlhttp://....../a.asmx/webservice方法名

body使用json数据格式

xmlhttp.SetRequestHeader ("Content-Type","application/json; charset=utf-8");
    xmlhttp.SetRequestHeader ("Content-Length",getlen(body));

发表于 2014-11-27 09:26  柠茶  阅读(958)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章: