這篇文章主要介紹了js調(diào)用webservice構(gòu)造SOAP進(jìn)行身份驗(yàn)證的相關(guān)資料,需要的朋友可以參考下
本文實(shí)例為大家分享了js調(diào)用webservice構(gòu)造SOAP進(jìn)行身份驗(yàn)證的相關(guān)內(nèi)容,注釋清除,供大家參考,具體內(nèi)容如下
<html>
<head>
<title>無標(biāo)題頁</title>
<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>找不到請求的服務(wù)器資源!";
}
}
else if(xmlhttp.readyState==0)
{
h.innerHTML="<br>未初始化!";
}
else if(xmlhttp.readyState==1)
{
h.innerHTML="<br>正在加載……!";
}
else if(xmlhttp.readyState==2)
{
h.innerHTML="<br>已經(jīng)加載完成!";
}
else if(xmlhttp.readyState==3)
{
h.innerHTML="<br>正在和服務(wù)器交互";
}
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)) //匹配雙字節(jié)
{
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
//實(shí)際上這段代碼就是用JS構(gòu)造一個(gè)字符串,而這個(gè)字符串就是通過瀏覽器查看WEBSERVICE時(shí)方法時(shí)出現(xiàn)的那段
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/">';
//加了這段可以實(shí)現(xiàn)soap頭
//UserName ,Pwd這是我的SOAP驗(yàn)證形式,需替換
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);
alert(data);
}
//WeatherReport Test:
function Button2_onclick() {
//方法名,參數(shù)名,參數(shù)值,服務(wù)URL,服務(wù)所使用命名空間
RequestByPost("HelloWorld",[],[],"http://localhost:13267/SERVICES/WebService.asmx","http://tempuri.org/");
}
window.onload = function(){
Button2_onclick();
}
// ]]>
</script>
</head>
<body>
<label id='Label1'>ss</label>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)javascript程序設(shè)計(jì)有所幫助。