未捕获的TypeError:无法读取未定义的属性"URL"-Google Image API [英] Uncaught TypeError: Cannot read property 'url' of undefined - Google Image API
本文介绍了未捕获的TypeError:无法读取未定义的属性"URL"-Google Image API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试生成5个随机图像(第一行来自闪烁,工作正常).在第二行中,它来自Google,但由于某种原因它只返回4,并在控制台上返回错误,提示:
I am trying to generate 5 random images (first row is from flicker and worked ok). On the second row it's from google, but it's only returning 4 for some reason and returning an error on the console that says:
未捕获的TypeError:无法读取未定义的属性'url'
Uncaught TypeError: Cannot read property 'url' of undefined
这是我的HTML
<div class="welcomeScreen">
<form id="players">
<p>Player 1</p>
<input id="player1Name" placeholder="Enter player 1's name">
<div class='player1Info clearfix'>
<label>
<img src="">
<input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player1Avatar' class='player-1-avatar' value=''>
</label>
</div>
<p>Player 2</p>
<input id="player2Name" placeholder="Enter player 2's name">
<div class='player2Info clearfix'>
<label>
<img src="">
<input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
</label>
<label>
<img src="">
<input type='radio' name='player2Avatar' class='player-2-avatar' value=''>
</label>
</div>
<input value="Start the race!" type="submit">
</form>
</div>
JavaScript
Javascript
function buildFlickrUrl(p) {
var url = "https://farm";
url += p.farm;
url += ".staticflickr.com/";
url += p.server;
url += "/";
url += p.id;
url += "_";
url += p.secret;
url += ".jpg";
return url;
}
$(document).ready(function() {
var flickrUrl = "https://www.flickr.com/services/rest/?
method=flickr.photos.search&format=json&api_key=
4ef070a1a5e8d5fd19faf868213c8bd0&nojsoncallback=1&text=dog
$.get(flickrUrl, function(response) {
for(var i = 0; i < 5; i++) {
var photoUrl = buildFlickrUrl(response.photos.photo[i]);
$(".player1Info label img").eq(i).attr('src', photoUrl);
$(".player1 img").eq(i).attr('src' , photoUrl);
console.log(photoUrl);
}
});
var input="cute kitten";
$.getJSON("https://ajax.googleapis.com/ajax/services/search/images?callback=?", {
q: input,
v: '1.0'
},
感谢您的所有帮助!
推荐答案
在控制台中设置一个断点,然后查看返回的结果.
Set a breakpoint in the console and look at the results being returned.
> data.responseData.results
[Object, Object, Object, Object]
您将看到结果的长度为4,并且正在循环读取索引4.
You will see the results has a length of 4 and you are looping to read index of 4.
更改
for(var i = 0; i < 5; i++) {
到
for(var i = 0; i < data.responseData.results.length; i++) {
这篇关于未捕获的TypeError:无法读取未定义的属性"URL"-Google Image API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文