Dynamically add new HTML elements on page load after getting JSON data from Web API
- by Luis D Urraca
I'm building an hybrid Android App using Phonegap/Apache Cordova. My app should get data from my web api. I'm using JSON to serve the data to the APP. So i got a the following code:
function init() {
document.addEventListener('deviceready', onDeviceReady, false);
var url = "http://23.21.128.153:3000/regions.json";var jsonresults;
$.getJSON(url,function(data){
jsonresults = data;
$.each(jsonresults, function(i,v){
$('#main-content').append('<li>'+jsonresults[i].name+'</li>');
});
});
}
and also on the body of the html i have a div called main-content. Everythings works fine in the Eclipse browser, but on the Android Emulator is not working. Not sure if there's another way to pull data from Web API using JSON and dinamically create HMTL elements after getting the data.
https://gist.github.com/2956660