好的,我知道这听起来很愚蠢,因为下面的代码应该可以工作
document.getElementById('petimg').src = petJSON[0].picture[0].large;
但是它不断使网站崩溃
这是完整的代码
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://service.ipetfindr.com/iOS/?uri=fetchpet/13373A" + n[1],false);
xmlhttp.send();
var petJSON = JSON.parse(xmlhttp.responseText);
alert(petJSON[0].picture.large);
document.getElementById('petimg').src = petJSON[0].picture.[0].large;
document.getElementById("petname").innerHTML = petJSON[0].petname;
document.getElementById("breed").innerHTML = petJSON[0].breed;
document.getElementById("petid").innerHTML = petJSON[0].ipetfindrtagid;
我遇到问题的 JSON 部分如下
"picture":[{"large":"http:\/\/www.ipetfindr.com\/petuploads\/7b2b07363b271703782d0b7d5362f8f4.JPG","small":"http:\/\/www.ipetfindr.com\/petuploads\/7b2b07363b271703782d0b7d5362f8f4-x-h80.JPG"}]
EDIT FIX 修复此错误的方法很简单
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","http://service.ipetfindr.com/iOS/?uri=fetchpet/13373A" + n[1],false);
xmlhttp.send();
var petJSON = JSON.parse(xmlhttp.responseText);
var petpicture = petJSON[0].picture[0];
alert(petpicture.large); //THIS allows me or anyone to call the large image in the sub array
document.getElementById('petimg').src = petJSON[0].picture.[0].large;
document.getElementById("petname").innerHTML = petJSON[0].petname;
document.getElementById("breed").innerHTML = petJSON[0].breed;
document.getElementById("petid").innerHTML = petJSON[0].ipetfindrtagid;