javascript - how to get result of getjson promise inside $.when function? -
using following code try data of 2 getjson calls array when both getjson called completed. code give me error:
resultfromurl1.feed undefined var entry1 = resultfromur11.feed.entry;
i looked @ f12 saw both getjson executed no data put array! how fix error ? furthermor should use $.when(url1promise, url2promise).then or $.when(url1promise, url2promise).done ?
<javascript> var files = new array(); function pushtoarray() { //first getjson call var url1 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; var url1promise = $.getjson(url1, function (data) { console.log("url1 success"); });//end of ajax call //second getjson call var url2 = "https://spreadsheets.google.com/feeds/list/xxxxx/xxxxx/public/values?alt=json"; var url2promise = $.getjson(url2, function (data) { console.log("url2 success"); });//end of function $.when(url1promise, url2promise).then(function (resultfromurl1, resultfromurl2) { var entry1 = resultfromurl1.feed.entry; var entry2 = resultfromurl2.feed.entry; var entry = entry1.concat(entry2); $(entry).each(function () { // column names name, age, etc. count++; files.push({ url: this.gsx$url.$t, filename: this.gsx$name.$t }); alert(files.length); print_r(files); console.log(files); }); }); }//end of function </javascript> <body onload="pushtoarray()">
jquery ajax functions bit of pain use $.when()
. is in resultfromurl1
array of 3 values this: [data, textstatus, jqxhr]
.
so, need change to:
var entry1 = resultfromurl1[0].feed.entry; var entry2 = resultfromurl2[0].feed.entry;
in addition, should add error handler $.when().then()
construct can see if errors occurred in getjson()
calls.
Comments
Post a Comment