简体   繁体   English

如何使用Ajax从#facebook graph url查找返回的json对象获取特定字段

[英]How to get particular field from a json object returned by # facebook graph url lookup using ajax

I am trying to get a particular field values from a json object returned facebook graph api using javascript ajax call. 我正在尝试使用javascript ajax调用从返回的json对象获取一个特定的字段值,该值返回Facebook图形api。

I am trying following code to get the cover field from json object but its not accessible . 我正在尝试按照以下代码从json对象获取Cover字段,但它不可访问。 for referenced http://jsfiddle.net/GD4PC/ her is the code 对于引用的http://jsfiddle.net/GD4PC/她是代码

    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 ) {
       if(xmlhttp.status == 200){
           var ur="https://facebook.com/344128252278047";
           var res= JSON.parse(xmlhttp.responseText);
           console.log(res);
           document.getElementById("myDiv").innerHTML=res.cover;

       }
       else if(xmlhttp.status == 400) {
          alert('There was an error 400')
       }
       else {
           alert('something else other than 200 was returned')
       }

If you will look at your content of res you will know the cover field is not directly accessible. 如果您查看自己的res内容,将知道cover字段不可直接访问。

To access cover try this. 要访问cover尝试此操作。

res[ur].cover

To access that source which you've mentioned in comment you should say 要访问您在评论中提到的source ,您应该说

res[ur].cover.source

Because your object has a field "https://facebook.com/344128252278047" which is same as ur . 因为您的对象有一个字段"https://facebook.com/344128252278047" ,它与ur相同。 In that field there is a field cover . 在那个领域有一个领域cover

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM