0

I am receiving the following successful response :

{"123abc": {"lat": 101.45, "lon": 777.87},"uid345":{"lat":12.09,"lon":98.08}}

After posting this jquery ajax request :

$.ajax({
    url: postUrl,
    type: 'POST',
    beforeSend: function () {
        $.mobile.loading('hide');
    },
    complete: function () {
        $.mobile.loading('hide');
    },
    crossDomain: true,
    data: {
        lat: 101.45,
        lon: 777.87,
        uid: '123abc'
    },
    dataType: 'json',
    success: function (dataString) {
        alert(dataString);
    },
    error: function (error) {}
});

alert(datastring); prints [object object] in a alert window.

How can I process the received response?

p.s: I just completed a udacity web development course and this is new to me.

Damon
  • 3,004
  • 7
  • 24
  • 28
Harshit
  • 1,207
  • 1
  • 20
  • 40
  • access properties as `datastring.lat` Or `datastring.[0].lat`. You'll get the gist how to access other's – Ani Dec 09 '13 at 20:39
  • jQuery Mobile isn't involved in your question. – Omar Dec 09 '13 at 20:51
  • FYI, that's to be expected. `alert` converts the argument to a string and the default string representation of an object is `[object Object]`. – Felix Kling Dec 09 '13 at 20:53
  • thanks for the hints and help , I will refer to the above link and sort it out . This will be more than enough – Harshit Dec 09 '13 at 20:54

2 Answers2

1

Use the console to check your response, that way you can expand the object out and what fields are available:

console.log(dataString);

There are a number of options for processing depending on what you want to do. The most used is iterating over your objects keys and displaying the appropriate info via a for in or $.each loop.

To iterate the lat and lon of each object, something like below will work:

for (var key in dataString) {
    console.log(dataString[key].lat);
    console.log(dataString[key].lon);
}
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • is it possible to have all the latitudes and longitudes in a seprate list like python . I want to store them and print them as marker on google map – Harshit Dec 09 '13 at 20:51
1

access properties as datastring.lat Or datastring.[0].lat. You'll get the gist how to access other's

If the later is true,

Do loop on datastring till datastring.length

Ani
  • 4,473
  • 4
  • 26
  • 31