admin管理员组文章数量:1415059
I need to parse the xml response returned by a web service in ajax, this is my code, 'response' is the response returned by the web service, how do i create a xml object and parse it?
$.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);
}
});
This is my XML code:
<ArrayOfMobileConfiguration xmlns:xsd="w3/2001/XMLSchema"; xmlns:xsi="w3/2001/XMLSchema-instance"; xmlns="tempuri/">;
<MobileConfiguration>
<Id>1</Id>
<Key>STMaxDownloadSize</Key>
<Value>132000</Value>
</MobileConfiguration>
<MobileConfiguration>
<Id>2</Id>
<Key>ZoomingThresholdValue</Key>
<Value>14</Value>
</MobileConfiguration>
</ArrayOfMobileConfiguration>
I need to parse the xml response returned by a web service in ajax, this is my code, 'response' is the response returned by the web service, how do i create a xml object and parse it?
$.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);
}
});
This is my XML code:
<ArrayOfMobileConfiguration xmlns:xsd="w3/2001/XMLSchema"; xmlns:xsi="w3/2001/XMLSchema-instance"; xmlns="tempuri/">;
<MobileConfiguration>
<Id>1</Id>
<Key>STMaxDownloadSize</Key>
<Value>132000</Value>
</MobileConfiguration>
<MobileConfiguration>
<Id>2</Id>
<Key>ZoomingThresholdValue</Key>
<Value>14</Value>
</MobileConfiguration>
</ArrayOfMobileConfiguration>
Share
Improve this question
edited May 7, 2015 at 10:55
Rory McCrossan
338k41 gold badges320 silver badges351 bronze badges
asked May 7, 2015 at 10:50
MaloMalo
1,6164 gold badges22 silver badges36 bronze badges
2
- Try DomParser. – Nilay Vishwakarma Commented May 7, 2015 at 10:55
- stackoverflow./questions/17604071/parse-xml-using-javascript – Garry Commented May 7, 2015 at 11:00
3 Answers
Reset to default 4jQuery is able to retrieve values from an XML response in the same manner it would select standard HTML. Try this:
$.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);
}
});
This:
dataType: 'xml:lang',
should be just:
dataType: 'xml',
jQuery will then ignore the content-type of the response and parse it as XML before populating the variable you have named response
in the success
function with it.
Try the following code,
$.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);
}
});
本文标签: jqueryparse xml response of web service using javascript and ajaxStack Overflow
版权声明:本文标题:jquery - parse xml response of web service using javascript and ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745231070a2648825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论