我试图从json对象获取一个特定的字段值,使用javascript ajax调用返回facebook graph api。
我正在尝试使用代码从json对象获取封面字段,但无法访问它。 引用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')
}
答案 0 :(得分:1)
如果您要查看res
的内容,则会知道cover
字段无法直接访问。
要访问cover
,请尝试此操作。
res[ur].cover
要访问您在评论中提及的source
,您应该说
res[ur].cover.source
因为您的对象的字段"https://facebook.com/344128252278047"
与ur
相同。在该字段中有一个字段cover
。