多的不说,先贴代码
function RequestWebService() {
try {
var time1=new Date().getTime();//进行效能分析
var domain = document.domain;//获取文档域名
var URI = "http://" + domain + ":8082/Service1.asmx";//设定请求文档的路径
var data="";//存储soap协议文档
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
data = data + '<soap12:Body>';
data = data + '<HelloWorld xmlns="http://tempuri.org/" >';
data = data + '<value>' + document.getElementById("a").value.trim() + '</value>';
data = data + '</HelloWorld>';
data = data + '</soap12:Body>';
data = data + '</soap12:Envelope>';
var xmlhttp;//构建ajax异步对象
if (XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
//xmlhttp.Open("POST", URL, false);
xmlhttp.open("post", URI, false);//设定为非异步请求,post方法
//xmlhttp.SetRequestHeader("Content-Type", "application/soap+xml");
xmlhttp.setRequestHeader("Content-Type", "application/soap+xml");//设定请求头,内容类型为,SOAP+xml格式
xmlhttp.send(data);//提交请求
document.getElementById("data").innerHTML = xmlhttp.responseText;
console.log(xmlhttp.responseText);
var time2=new Date().getTime();//<span style="font-family: Arial, Helvetica, sans-serif;">进行效能分析</span><pre name="code" class="javascript"><span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span> console.log(time2-time1);//向控制台打印出所用时间</span>
} catch (er) { console.log(er.message); } }
实质上js请求webservice服务是一个post请求,使用jquery的post方法更为简单些。