我正在请求外部api,它给出了以下回复。它采用XML格式。
这是方式,我如何使用
$('#trigger').click(function()
{
var token = '662d3330f192b4af77d5eef8de58f7e8c01a12a7';
var location = $('#Address').val();
var lat = $('#latitude').val();
var long = $('#longitude').val();
$.ajax({
url: 'https://api.quickblox.com/data/Location',
data: {
token:'2100d8b55d4a389470b849de9378093dcda12903'
},
method: 'get',
success: function(xml) {
console.log(xml);
}
});
});
这是我从服务器获得的响应。
#document
<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>
<Location>
<_id>558b8d97535c129bd3000e04</_id>
<_parent_id>test-api</_parent_id>
<created-at type="integer">1435209111</created-at>
<lat nil="true"/>
<location>Hyderabad, Telangana, India</location>
<long nil="true"/>
<updated-at type="integer">1435209111</updated-at>
<user-id type="integer">3566216</user-id>
</Location>
<Location>
<_id>558b8d81535c12aadd046730</_id>
<_parent_id>test-api</_parent_id>
<created-at type="integer">1435209089</created-at>
<lat type="float">13.1067448</lat>
<location>Avadi, Tamil Nadu, India</location>
<long type="float">80.0969511</long>
<updated-at type="integer">1435209089</updated-at>
<user-id type="integer">3566216</user-id>
</Location>
<Location>
<_id>558b86ad535c123883005741</_id>
<_parent_id>test-api</_parent_id>
<created-at type="integer">1435207341</created-at>
<lat type="float">10.3673123</lat>
<location>Dindigul, Tamil Nadu, India</location>
<long type="float">77.9802906</long>
<updated-at type="integer">1435207341</updated-at>
<user-id type="integer">3566216</user-id>
</Location>
<Location>
<_id>558b8f72535c126322011368</_id>
<_parent_id>test-api</_parent_id>
<created-at type="integer">1435209586</created-at>
<lat type="float">12.977357</lat>
<location>Bangalore City Junction, Gubbi Thotadappa Road, Majestic, Bengaluru, Karnataka, India</location>
<long type="float">77.57077</long>
<updated-at type="integer">1435209586</updated-at>
<user-id type="integer">3566216</user-id>
</Location>
</data>
以下是图片的快照
如何从响应中获取_id,created_at字段?
我在成功功能
中尝试过这样的功能 success: function(xml) {
console.log(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());
});
}
但我没有得到任何东西。我怎么能这样做?
答案 0 :(得分:0)
试试这个:
success: function(xml) {
console.log(xml);
var xmlDoc = $.parseXML(xml);
$(xmlDoc).find("Location").each(function(idx, value) {
var id = $(value).find("_id").text();
var parentId = $(value).find('_parent_id').text();
var createdAt = new Date(parseInt($(value).find('created-at').text()));
var lat = parseFloat($(value).find('lat').text());
});
}
答案 1 :(得分:0)
最好将json与web
一起使用所以替换
url: 'https://api.quickblox.com/data/Location'
与
url: 'https://api.quickblox.com/data/Location.json'
可以毫无问题地访问JSON