我正在尝试向 api 发送请求,但我得到的响应很奇怪。
我将回复作为图片附上。
这是文本中的真实 XML 响应
<data type="array" class_name="Location" skip="0" limit="0">
<Location>
<_id>558a8325535c1246bb00d5c5</_id>
<_parent_id>test-api</_parent_id>
<created-at type="integer">1435140901</created-at>
<lat type="float">11.0270643</lat>
<location>Avarampalayam, Coimbatore, Tamil Nadu, India</location>
<long type="float">76.9830277</long>
<updated-at type="integer">1435140901</updated-at>
<user-id type="integer">3566216</user-id>
</Location>
<Location>
<_id>558a83dd535c12843900dbbe</_id>
<_parent_id>test-api</_parent_id>
<created-at type="integer">1435141085</created-at>
<lat type="float">11.0310806</lat>
<location>Mettupalayam Bus Stand, Mettupalayam Road, Tatabad, Coimbatore, Tamil Nadu, India</location>
<long type="float">76.9525282</long>
<updated-at type="integer">1435141085</updated-at>
<user-id type="integer">3566216</user-id>
</Location>
</data>
在 #document
下
我如何获取上述给定响应中的值?
这是我用来请求的方式
$.ajax({
url: 'https://api.quickblox.com/data/Location',
data: { token:'662d3330f192b4af77d5eef8de58f7e8c01a12a7' },
method: 'get',
success: function(msg) {
var value = msg;
console.log(msg);
}
})
最佳答案
您可以对返回的 XML 使用 jQuery 的标准 DOM 遍历函数来检索您需要的值。试试这个:
$.ajax({
url: 'https://api.quickblox.com/data/Location',
data: {
token:'662d3330f192b4af77d5eef8de58f7e8c01a12a7'
},
method: 'get',
dataType: 'xml', // note this is optional as jQuery should detect and parse it for you automatically
success: function(xml) {
$(xml).filter('data').find('Location').each(function() {
var id = $(this).find('_id').text();
var parentId = $(this).find('_parent_id').text();
var createdAt = new Date($(this).find('created-at').text());
var lat = parseFloat($(this).find('lat').text());
// retrieve the other properties...
// work with them here...
});
}
});
关于php - 解析 xml 响应奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024364/