XMLHttpRequest 对象异步调用web服务

触发事件:

ο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; }

转载于:https://www.cnblogs.com/johan/archive/2010/03/06/1948002.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值