在JavaScript中访问范围之外的Instagram数据

时间:2016-12-28 13:22:49

标签: javascript scope instagram

我只需要在我的范围之外访问我的instgram数据,请帮助我。一切都很适合我

$(document).ready(function ()
     {
             var apiurl = "https://api.instagram.com/v1/users/4322593457/media/recent/?access_token=4322593457.15d3a7f.13779606843446ab834b0e8512412d4a&count=5&callback=?";
             $.getJSON(apiurl, function (data) {

                  suatroot = data.data;
                 $.each(suatroot, function (key, val) {

                     var itemobj = val.images.low_resolution.url;
                     var hrefobj = val.link;
                     var captobj = val.caption.text;
                      data = captobj;  //Can I access this???????
                     var suatpaket = "<a target='_blank' href='"+hrefobj+"'><img src='" + itemobj + "'/><br>"+captobj+"<br></a>";
                     $(".instagram").append(suatpaket);
                 });
             });
         });
Console.log(suatroot); //undefined  here I want object

2 个答案:

答案 0 :(得分:0)

创建一个接受suatroot作为参数的函数。同样明智:

function suatrootCallback(suatroot){

   // code to handle suatroot/data.data

}

$(document).ready(function (){

             var apiurl = "https://api.instagram.com/v1/users/4322593457/media/recent/?access_token=4322593457.15d3a7f.13779606843446ab834b0e8512412d4a&count=5&callback=?";
             $.getJSON(apiurl, function (data) {

                 var suatroot = data.data; // do not create global variable
                 suatrootCallback(suatroot); // call the callback

                 $.each(suatroot, function (key, val) {

                     var itemobj = val.images.low_resolution.url;
                     var hrefobj = val.link;
                     var captobj = val.caption.text;
                      data = captobj;  //Can I access this???????
                     var suatpaket = "<a target='_blank' href='"+hrefobj+"'><img src='" + itemobj + "'/><br>"+captobj+"<br></a>";
                     $(".instagram").append(suatpaket);
                 });
             });
});

答案 1 :(得分:0)

您只能在初始化后才能访问Instagram数据,因此您可以在响应后调用函数。

$(document).ready(function () {
    var apiurl = "https://api.instagram.com/v1/users/4322593457/media/recent/?access_token=4322593457.15d3a7f.13779606843446ab834b0e8512412d4a&count=5&callback=?";
    $.getJSON(apiurl, function (data) {
        suatroot = data.data;
        $.each(suatroot, function (key, val) {
            var itemobj = val.images.low_resolution.url;
            var hrefobj = val.link;
            var captobj = val.caption.text;
            data = captobj; 
            callbackInstagram(data);    //accessed here
            //window.data = data        also if you want to access if globally after initailized
            var suatpaket = "<a target='_blank' href='"+hrefobj+"'><img src='" + itemobj + "'/><br>"+captobj+"<br></a>";
            $(".instagram").append(suatpaket);
        });
    });
});



function callbackInstagram (data) {
    console.log(data);
}

希望有所帮助!感谢