Hey guys ^^ a link to my twitch tv app. I have made an array with variable name arr. It is declared at the top. I use getJSON method to pass display names of all the user inside the variable arr. I am unable to display the names stored in arr outside getJSON method and I am unable to identify the problem
Please help me.
$.getJSON()
is an asynchronous operation - meaning, your app will continue executing code below this function before the callback is triggered. Solution: continue the rest of your app from the getJSON callback.
function updateView(data) {
// app continues from this point
}
$.getJSON(url, function(data) {
// consider calling another function passing in data
updateView(data)
});
@0x0936
Thank you for your help. Following your advice, I made this function
// app continues from this point
var j=user.length;
if (arr.length===j){
console.log(arr);}
} ```
and it worked.
My knowledge on JSON and Ajax is weak. Can you please advice me on where can I learn more about these topics?
Thanks
I learned through trial and error, through the fire and flames. I spent a lot of time reading documentation, blog posts, stack overflow answers, and FCC chat. So, unfortunately I don’t have a single resource that can easily explain how to use front-end APIs and how to troubleshoot common problems (usually CORS). But, I can give you a few links that have helped me.
- jQuery $.ajax() - learn this one, seriously
-
jQuery $.getJSON() - this is a short-hand, convenience method for
$.ajax()
and uses some sane defaults - but you may not always want or need these defaults
// $.getJSON() under the hood
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
-
JSONP, no link - just Google and practice. The gist is you can append to the query parameters
&callback=?
to ask the server for a jsonp response. If this is the only query parameter, it looks like this:?callback=?
. JSONP makes front-end API requests much easier to handle, because many API servers don’t send anAccess Control Allow Origin
header. -
if your web app is using
https
, then so should all of your other links to external resources
thanks a lot, really appreciate your help!