我正在尝试通过我得到的响应向api发送请求很奇怪。
我将回复附加为图片。
以下是文本中的Real XML Response
<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);
}
})
答案 0 :(得分:3)
您可以在返回的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...
});
}
});
答案 1 :(得分:1)
正确的方法是在网络上使用JSON而不是XML
首先用
替换网址url: 'https://api.quickblox.com/data/Location.json'
然后您只需访问所需的任何密钥
还有一个Web SDK http://quickblox.com/developers/Javascript 所以你可以用它来发出请求