JS調用webservice的兩種方式

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   sp   for   strong   on   

協議肯定是使用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();  

url:http://....../a.asmx/webservice方法名

body使用json資料格式

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

JS調用webservice的兩種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.