wiwo wiwo - 23 days ago 22
jQuery Question

Simple $.getJSON("adress"),function(res){ does not wok

I have a simple function:

setInterval(function () {
$.getJSON("https://graph.facebook.com/v2.8/?ids=1234567_1234567&fields=reactions.type(LIKE).limit(0).summary(total_count).as(reactions_like)%2Creactions.type(LOVE).limit(0).summary(total_count).as(reactions_love)&access_token=thatsmytoken"),
function (res) {

console.log(res);

};
}, 5000);


So it should call GET and show the result in console but its not...
I can see the result of GET in network info in Chrome (every 5 seconds it works fine) but its not going inside the function(res)

Why is that?

Answer

Your parentheses are incorrect. It should be:

setInterval(function () {
    $.getJSON("https://graph.facebook.com/v2.8/?ids=1234567_1234567&fields=reactions.type(LIKE).limit(0).summary(total_count).as(reactions_like)%2Creactions.type(LOVE).limit(0).summary(total_count).as(reactions_love)&access_token=thatsmytoken",
        function (res) {

            console.log(res);

        });
}, 5000);

Now, function(res){...} will be the callback of the getJSON function if it succeeds. Click here for more information about the callbacks.