javascript - jQuery JSON get issue -


i trying fetch information json file nothing comes in data variable. can tell me doing wrong. json file downloaded there no problem me not getting server.

function handle_geolocation_query(position) {     var url = "http://ws.geonames.org/findnearbyweatherjson?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";     $.getjson(url, function(info){         var clouds = info.weatherobservation.clouds;         var weather = info.weatherobservation.weathercondition;         var temp = info.weatherobservation.temperature;         var humidity = info.weatherobservation.humidity;     });     //console.log(clouds);     document.getelementbyid('result').innerhtml = "c:" + clouds + ", w:" + weather + ", t:" + temp + ", h:" + humidity; } 

appreciate help. lot.

you should try placing code modifies innerhtml within callback. remember js has function scope. can't access these variables outside. also, variables set after callback retrieving json executed.

function handle_geolocation_query(position) {     var url = "http://ws.geonames.org/findnearbyweatherjson?lat=" + position.coords.latitude + "&lng=" + position.coords.longitude + "&callback=?";     $.getjson(url, function(info){         var clouds = info.weatherobservation.clouds;         var weather = info.weatherobservation.weathercondition;         var temp = info.weatherobservation.temperature;         var humidity = info.weatherobservation.humidity;         $('#result').html("c:" + clouds + ", w:" + weather + ", t:" + temp + ", h:" + humidity);     }); } 

edit: following gov's advice, should use jquery api's encapsulated html() method whenever find need change innerhtml.

also, can use:

console.log(info); 

while debugging in getjson callback ensure data structured way think is.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -