刚开始接触ajax的时候好蛋疼啊,还是jquery用着爽。各位看官可以忽略此篇了。
调用:
RequestByPost(method,variable,value,url,_Namespace)
method:webservice的方法名
variable:webservice的方法的变量名数组
value:webservice的方法的变量的值的数组
url:请求的地址(asmx文件地址)
_Namespace:webservice的命名空间
<script language="javascript" type="text/javascript">
// <!CDATA[
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//define
var xmlhttp;
var value=new Array();
var variable=new Array();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//Show Response MSG.
function handleStateChange()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
var h=document.getElementById("Label1");
if(xmlhttp.readyState==4)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(xmlhttp.status==200)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert(xmlhttp.responseText);
h.innerHTML=xmlhttp.responseText;
//h.innerHTML=xmlhttp.responseXML;
}
else if(xmlhttp.status==404)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h.innerHTML="<br>找不到请求的服务器资源!";
}
}
else if(xmlhttp.readyState==0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h.innerHTML="<br>未初始化!";
}
else if(xmlhttp.readyState==1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h.innerHTML="<br>正在加载……!";
}
else if(xmlhttp.readyState==2)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h.innerHTML="<br>已经加载完成!";
}
else if(xmlhttp.readyState==3)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h.innerHTML="<br>正在和服务器交互";
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
h.innerHTML=xmlhttp.responseXML;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//Get Request Data's length
function getlen(str)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
var bytesCount=0;
for (var i = 0; i < str.length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
var c = str.charAt(i);
if (/^[\u0000-\u00ff]$/.test(c)) //匹配双字节
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bytesCount += 1;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
bytesCount += 2;
}
}
return bytesCount;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//Create XMLHttpRequest Object
function createXMLHttpRequest()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(window.ActiveXObject)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequst)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xmlhttp=new XMLHttpRequest();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//send Request By HTTP POST
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
function RequestByPost(method,variable,value,url,_Namespace)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
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:Body>';
data = data + '<'+method+' xmlns="'+_Namespace+'">';
for(var i=0;i<variable.length;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
data = data + '<'+variable[i]+'>'+value[i]+'</'+variable[i]+'>';
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
data = data + '</'+method+'>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//CallHelloWorld!
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function SayHello_onclick()
{
//alert(document.getElementById('YourName').value);
RequestByPost("HelloWorld",new Array("msg"),new Array(document.getElementById('YourName').value),"WebService.asmx","localhost/");
}
//WeatherReport Test:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function Button2_onclick()
{
RequestByPost("getWeatherbyCityName",new Array("theCityName"),new Array(document.getElementById('CityName').value),"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx","http://WebXml.com.cn/");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// ]]>
</script>