未捕获的TypeError:无法读取未定义的属性'url' - Google Image API

时间:2014-11-14 19:51:09

标签: javascript jquery api

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

未捕获的TypeError:无法读取未定义的属性'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'
}, 

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

在控制台中设置断点并查看返回的结果。

> 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++) {

答案 1 :(得分:1)

您的回复数据还可以,问题出在您的for循环中。

对象data.responseData.results包含4个元素和循环,因为 i&lt; 5 即可。只需将其更改为data.responseData.results.length

 for(var i = 0; i < data.responseData.results.length ; i++) {
    var googleImageUrl = data.responseData.results[i].url;
    $(".player2Info label img").eq(i).attr('src', data.responseData.results[i].url);
    console.log(googleImageUrl);
  }