有什么问题。
A是能够列出了一堆的照片,但我无法使搜索工作。收到此错误:
遗漏的类型错误:无法读取属性“小”的未定义
xhr.onload @ main.js:15
负载(异步)
loadImages @ main.js:7
document.getElementById("button").addEventListener("click", loadImages);
function loadImages() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.unsplash.com/search/photos?page=1&query=office&client_id=15020f1f31839a088aff745486e7a469cd064761ff165c9d3d9f57de77d10348", true);
xhr.onload = function() {
if(this.status == 200) {
var images = JSON.parse(this.responseText);
var output = "";
for(var i in images) {
output +=
'<div class="image">' +
'<img src="'+images[i].urls.small+'">'
'</div>';
}
document.getElementById("images").innerHTML = output;
console.log(images);
}
}
xhr.send();
}
基本上,在更改该网址之前一切正常(第一个链接有效,第二个链接无效)。但唯一不同的是...
https://api.unsplash.com/photos?client_id=
https://api.unsplash.com/search/photos?page=1&query=office&client_id=
最佳答案
您希望:
[
{
"urls": {
"small": ...
...
},
....
},
...
]
{
"total": 8519,
"total_pages":852,
"results": [
{
"urls": {
"small": ...
...
},
....
},
...
],
...
}
var images = JSON.parse(this.responseText);
你应该写:
var response = JSON.parse(this.responseText);
var images = response.results;
关于javascript - 无法读取的XMLHttpRequest.xhr.onload未定义的属性(Unsplash API),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53144611/