According to my previous post I am trying to get back some information from a RESTful interface using JSON. I am working on JQuery 1.5.
The problem I face is that I get back as a result undefined values. I am not able to use firebug because my application is developed using PhoneGap and the app is running on the iPhone's simulator.
If I visit the RESTful interface (I type the "example.json" url in a browser - where example is a valid url created by ano开发者_开发百科ther developer) returns me results with the following format:
[{"person":{"created_at":"2011-07-18T17:51:33Z","id":1,"name":"John","age":60,"surname":"Smith","car_id":1,"updated_at":"2011-07-18T17:51:33Z"}},{"person":{"created_at":"2011-07-18T17:51:35Z","id":1,"name":"Johnny","age":50,"surname":"Deep","car_id":2,"updated_at":"2011-07-18T17:51:35Z"}}]
I need to get the information id, name, age and store them in an array (not an html table). Just to see if the connection returns any values I use the code:
var jqxhr = $.getJSON("example.json", function(person) {
$.each(person, function(i, person) {
alert(person.name);
alert(person.age);
});
})
.success(function() { alert("second success"); })
.error(function() { alert("Cannot connect to the SWT's maps. Please try again later!"); })
.complete(function() { alert("complete"); });
So, why do I get by the alert undefined as values?
This code should work:
var jqxhr = $.getJSON("example.json", function(person) {
$.each(person, function(i, item) {
alert(item.person.name);
alert(item.person.age);
});
})
.success(function() { alert("second success"); })
.error(function() { alert("Cannot connect to the SWT's maps. Please try again later!"); })
.complete(function() { alert("complete"); });
are you sure of your code ? try:
var jqxhr = $.getJSON("example.json", function(data) {
$.each(data, function(i, item) {
alert(item.name);
alert(item.age);
});
}
Hope this helps
精彩评论