gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'url' of undefined - Google Image API

转载 作者:行者123 更新时间:2023-11-29 18:11:36 25 4
gpt4 key购买 nike

我正在尝试生成 5 个随机图像(第一行来自闪烁并且工作正常)。在第二行,它来自谷歌,但由于某种原因它只返回 4 并在控制台上返回错误:

未捕获的类型错误:无法读取未定义的属性“url”

这是我的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

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'
},

感谢您的帮助!

最佳答案

在控制台设置断点,查看返回结果。

> data.responseData.results
[Object, Object, Object, Object]

您将看到结果的长度为 4,并且您正在循环读取索引 4。

改变

for(var i = 0; i < 5; i++) {

for(var i = 0; i < data.responseData.results.length; i++) {

关于javascript - 未捕获的类型错误 : Cannot read property 'url' of undefined - Google Image API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26937781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com