Home >Web Front-end >JS Tutorial >js calls webservice to construct SOAP for authentication_javascript skills

js calls webservice to construct SOAP for authentication_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:03:301295browse

The example in this article shares with you the relevant content of js calling webservice to construct SOAP for authentication. The comments have been cleared for your reference. The specific content is as follows

<html>
<head>
 <title>无标题页</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>找不到请求的服务器资源!";
    }
  }
  else if(xmlhttp.readyState==0)
  {
    h.innerHTML="<br>未初始化!";
  }
  else if(xmlhttp.readyState==1)
  {
    h.innerHTML="<br>正在加载……!";
  }
  else if(xmlhttp.readyState==2)
  {
    h.innerHTML="<br>已经加载完成!";
  }
  else if(xmlhttp.readyState==3)
  {
    h.innerHTML="<br>正在和服务器交互";
  }
  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))  //匹配双字节
    {
 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

//实际上这段代码就是用JS构造一个字符串,而这个字符串就是通过浏览器查看WEBSERVICE时方法时出现的那段
function RequestByPost(method,variable,value,url,_Namespace)
{
 createXMLHttpRequest();
 var data;
 data = '<&#63;xml version="1.0" encoding="utf-8"&#63;>';
 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/">';
 
 //加了这段可以实现soap头

 //UserName ,Pwd这是我的SOAP验证形式,需替换
 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() {
  //方法名,参数名,参数值,服务URL,服务所使用命名空间
  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>

The above is the entire content of this article. I hope it will be helpful to everyone in learning javascript programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn