[英]parse xml response of web service using javascript and ajax
我需要解析ajax中Web服务返回的xml响应,这是我的代码,“ response”是Web服务返回的响应,我如何创建xml对象并进行解析?
$.ajax({
type: 'POST',
url: 'web service link',
dataType: 'xml:lang',
success: function (response) {
// how to parse the response here
},
error: function (error) {
console.log(error);
}
});
这是我的XML代码:
<ArrayOfMobileConfiguration xmlns:xsd="w3.org/2001/XMLSchema"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns="tempuri.org/">;
<MobileConfiguration>
<Id>1</Id>
<Key>STMaxDownloadSize</Key>
<Value>132000</Value>
</MobileConfiguration>
<MobileConfiguration>
<Id>2</Id>
<Key>ZoomingThresholdValue</Key>
<Value>14</Value>
</MobileConfiguration>
</ArrayOfMobileConfiguration>
jQuery能够以与选择标准HTML相同的方式从XML响应中检索值。 尝试这个:
$.ajax({
type: 'POST',
url: 'web service link',
dataType: 'xml',
success: function (response) {
$('MobileConfiguration', response).each(function() {
var id = $(this).find('Id').text();
var key = $(this).find('Key').text();
var value = $(this).find('Value').text();
console.log(id, key, value);
});
},
error: function (error) {
console.log(error);
}
});
这个:
dataType: 'xml:lang',
应该只是:
dataType: 'xml',
然后jQuery将忽略响应的内容类型,并将其解析为XML,然后再在success
函数中使用它填充您为response
命名的变量。
试试下面的代码,
$.ajax({
type: 'POST',
url: 'web service link',
dataType: 'xml',
success: function (response) {
$(response).find('MobileConfiguration').each(function(){
var Id = $(this).find('Id').text();
var Key = $(this).find('Key').text();
var Value = $(this).find('Value').text();
// Use Id, Key, Value according to your requirement.
});
},
error: function (error) {
console.log(error);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.