使用 jQuery/JavaScript 从 SOAP 响应解析 XML
Parsing XML from SOAP response with jQuery/JavaScript
我在解析来自 SOAP 服务器的响应时遇到问题 jQuery。我想将XML响应转换为数组,因为有多行数据,如下所示。
这是请求:
<?xml version="1.0" encoding="utf-8"?>
<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:Body>
<GetWorkPos xmlns="http://localhost/apps">
<id>int</id>
</GetWorkPos>
</soap:Body>
</soap:Envelope>
这是回应:
<?xml version="1.0" encoding="utf-8"?>
<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:Body>
<GetWorkPosResponse xmlns="http://localhost.com/apps">
<GetWorkPosResult>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<quantity>decimal</quantity>
<Em>string</Em>
<type>string</type>
</GetWorkPos>
<GetWorkPos>
<ProductId>string</ProductId>
<Product>string</Product>
<Quantity>decimal</Quantity>
<Em>string</Em>
<Type>string</Type>
</GetWorkPos>
</GetWorkPosResult>
</GetWorkPosResponse>
</soap:Body>
</soap:Envelope>
这是我的代码:
$(document).ready(function () {
$("#send").click(function (event) {
var wsUrl = "http://localhost/Service.asmx";
var soapRequest =
'<?xml version="1.0" encoding="utf-8"?> '
<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:Body> '
<GetWorkPos xmlns="http://localhost.com/apps"> '
<id>' + $("#id").val() + '</id> '
</GetWorkPos> '
</soap:Body> '
</soap:Envelope>';
console.log(soapRequest);
$.ajax({
type: "post",
url: wsUrl,
contentType: "text/xml",
dataType: "xml",
data: soapRequest,
success: processSuccess,
error: processError
});
});
});
function processSuccess(data, status, req, xml, xmlHttpRequest, responseXML) {
$(req.responseXML)
.find('GetWorkPosResult')
.each(function(){
var id = $(this).find('ProductId').text();
console.log(id);
});
}
function processError(data, status, req) {
alert(req.responseText + " " + status);
console.log(data);
console.log(status);
console.log(req);
}
http://jsbin.com/uwirux/edit#javascript,html
var myObj = new Array();
$(req.responseXML)
.find('GetWorkPosResult').find('GetWorkPos')
.each(function(){
myObj.push($(this)); // Should't use .text() because you'll lose the ability to use .find('tagName')
});
for(var i = 0; i<myObj.length;i++){
var x = myObj[i].find('ProductId').text();
var y = myObj[i]find('Product').text();
}
或
$(myObj).each(function(){
var x = $(this).find('ProductId').text();
var y = $(this).find('Product').text();
});
相关文章:
- 显示IIS上javascript文件(SOAP请求)的XML响应
- RSS提要响应不是以原始XML格式提供的
- XMLHttpRequest获取请求xml响应
- dojo/request/iframe中读取xml响应的缺陷
- 从javascript向Ashx处理程序发送xml数据,并在新窗口中显示响应
- 将 XML 替换为来自 XMLHttprequest 的响应
- 从我的Java Web服务以REST响应(xml)发送图像
- 跨域 Post 方法 ajax 调用使用 jQuery 和 XML 响应
- $.ajax 响应在 content-Type:application/json 中为 JSON 中的 xml 内容生成
- Angularjs - 使用 $http 获取 xml 响应而不是 json
- 哪个“控制器”在 JSON 中转换来自 .NET Web 服务的 XML 响应
- 在IE9中解析xml/json响应
- AJAX 检索 xml 响应
- 尝试追加到文档时响应 XML 中缺少头部子项
- 显示对象.响应 XML 文本以进行调试
- SOAP对JSON的响应(XML)
- 响应XML为空,尽管responsetext是有效的.net webservice
- 在http响应xml中处理unicode
- 响应 XML 为空
- 通过使用javascript发出HTTP GET请求来显示响应(xml)