2

I am trying to send request to an api by what i get response is strange one.

I am attaching the response as image.

enter image description here

Here is the Real XML Response in text

<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>

Which comes under #document

How can i fetch the values in the above given response?

Here is the way i use to request

$.ajax({
    url: 'https://api.quickblox.com/data/Location',
    data: { token:'662d3330f192b4af77d5eef8de58f7e8c01a12a7' },
    method: 'get',
    success: function(msg) {
        var value = msg;
        console.log(msg);
    }
})
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
AngularAngularAngular
  • 3,589
  • 5
  • 25
  • 41
  • 2
    Maybe I'm wrong but It's not JSON, it's XML. And as far as I know the `#document` is just an addition of the browser. Take a look how the data really look in your `network` tab. – Ofir Baruch Jun 24 '15 at 10:39
  • Funny looking JSON you've got there. To get the values you simply need to traverse the *XML* that's returned. If you add your code which is making the request to the question, we can show you how to do this. – Rory McCrossan Jun 24 '15 at 10:40
  • @OfirBaruch Extremely sorry, its xml, got it,, updated – AngularAngularAngular Jun 24 '15 at 10:44
  • @RoryMcCrossan Updated the Question :) – AngularAngularAngular Jun 24 '15 at 10:44
  • As mentioned earlier, the `#document` isn't really in the response, it's just an addition of the browser. In order to work with XML, consider reading about `parseXML` https://api.jquery.com/jQuery.parseXML/. **Ignore** it and take a look @RoryMcCrossan's answer. – Ofir Baruch Jun 24 '15 at 10:49

2 Answers2

3

You can use jQuery's standard DOM traversal functions on the returned XML to retrieve the values you require. Try this:

$.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...
        });            
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

The right way is to use JSON instead of XML on web

So first of all replace urls with

url: 'https://api.quickblox.com/data/Location.json'

then you can simply access any key you need

Also there is a Web SDK http://quickblox.com/developers/Javascript so you can use it to make requests

Rubycon
  • 18,156
  • 10
  • 49
  • 70