触发事件: οnclick="GetProductsList();" 客户端js主体: var xmlhttp = false; //声明XML对象 var objXmlDoc; function getHTTPRequestObject() { //详见该系列前几篇文章 } function GetProductsList() { // 产生一个XMLHttpRequest对象实例 getHTTPRequestObject(); // 获取客户输入数据参数 var maxamount = document.getElementById("txtPrice").value; //设置web服务url位置和名字空间 var serviceUrl = "http://localhost/wsAdventureWorks/Service.asmx"; var serviceNamespace = "http://adventureworksSystem.com/Products" // 构建soap信封 var strEnvelope = "<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//">/n" + " <soap:Body>/n" + " <GetProducts xmlns=" + serviceNamespace + " >/n" + " <MaxAmount>" + maxamount + "</MaxAmount>/n" + " </GetProducts>" + " </soap:Body>" + "</soap:Envelope>"; var serviceAction = serviceNamespace + "/GetProducts"; //异步调用 xmlhttp.open("POST", serviceUrl, true); // 分配回调函数 xmlhttp.onreadystatechange = processResults; xmlhttp.setRequestHeader("Content-Type", "text/xml"); xmlhttp.setRequestHeader("SOAPAction", serviceAction); xmlhttp.send(strEnvelope); } function processResults() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { // 创建XML对象实例 objXmlDoc = new ActiveXObject("Msxml2.DOMDocument"); //XML对象加载内容 var serviceResponse = xmlhttp.responseText; objXmlDoc.loadXML(serviceResponse); if (objXmlDoc.parseError.errorCode != 0) //异常处理 { var xmlErr = objXmlDoc.parseError; alert("oops: " + xmlErr.reason); } else { ParseDataSet(); //解析xml中服务器返回的DataSet } } else { alert(xmlhttp.statusText); } } } function ParseDataSet() { objNodeList = objXmlDoc.getElementsByTagName("Products");//获取目标节点(数据表名) var stringout = ""; //解析vs web服务生成的表数据 for (var i = 0; i < objNodeList.length; i++) { dataNodeList = objNodeList[i]; var PriceNode = getParsedElement(dataNodeList, "ListPrice"); stringout = stringout + "..." + PriceNode + "..."; } document.getElementById("divResults").innerHTML = stringout;//前端显示解析的结果 } //返回目标位置指定名称的值 function getParsedElement(source, child) { var childNode = source.getElementsByTagName(child); return childNode[0].firstChild.nodeValue; }