2016-12-28 1 views
0

내 범위 밖의 내 인 그램 데이터에 액세스하면됩니다. 그래서 더 나은 당신이 함수를 호출 할 수 있습니다,자바 스크립트의 범위를 벗어난 Instagram 데이터에 액세스

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); 
       }); 
      }); 
}); 
+0

넣어 Console.log (suatroot); $ (document) .ready()의 끝에서 – Kenny

+0

자바 스크립트는 아약스 요청의 응답을 따르지 않습니다. 한 줄씩 실행합니다. 그래서 $ .getJson 자바 스크립트가 나올 때까지 기다리지 않고 Consolo.Log 코드와 suatroot 변수에 전달합니다. 변수는 여기에 정의되어 있지 않습니다. – Mehmet

답변

0

나를 위해 잘 작동한다 응답 후.

$(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); 
} 

희망 하시겠습니까? 감사합니다

+0

그리고 callbackInstagram (데이터) 함수에 액세스하는 방법 { console.log (data); } 외부에 있습니까? –

+0

@AnupChaudhary, 할 수 없습니다. AJAX 요청은 비동기식입니다. –

0

에만 initalized 된 후 인스 타 그램의 데이터에 액세스 할 수 있습니다 : 모든 매개 변수 현명한 등 suatroot을 허용하는 함수를 작성

$(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 
+0

그리고 어떻게 함수 callbackInstagram (데이터)에 액세스 할 수 있습니다 { console.log (data); } –

+0

은이 함수를'callbackInstagram()'이라고 부르거나, 실제로 그 데이터로 무엇을해야하는지 설명 할 수 있습니까? –