admin管理员组

文章数量:1400038

See this JSFiddle: /

If you have both Chrome and Firefox you can see that it works as expected in Chrome but in Firefox Firebug console you get "TypeError: e is undefined" On JSFiddle the error appears as "TypeError: obj is undefined"

I've spent hours hunting down this bug and trying to figure it out and finally gave up and came here. What's causing it?

Here is the full code to test:

<html>
<body>

  <div id="x"></div>

  <script src=".9.1/jquery.min.js"></script>
  <script type="text/javascript">

    $(document).ready(function(){
      getVideos('cats');
    });

    function getVideos(query){
      var url = ';maxResults=2&q='+query+'&regionCode=us&type=video&fields=items(id)&key=AIzaSyCCOnozV0lEfnjfMTjpc4TFExAeIGJ6Fh0';
      $.ajax({
        url: url,
        success: function(data){
          appendVideos(data);
        }
      });
    }

    function appendVideos(data){
      // works here
      console.log(data);

      // but not in $.each
      $.each(data.items, function(i, item){
        $('#x').append(item.id.videoId + '<br>');
      });
    }

  </script>

</body>
</html>

See this JSFiddle: http://jsfiddle/cDVQP/1/

If you have both Chrome and Firefox you can see that it works as expected in Chrome but in Firefox Firebug console you get "TypeError: e is undefined" On JSFiddle the error appears as "TypeError: obj is undefined"

I've spent hours hunting down this bug and trying to figure it out and finally gave up and came here. What's causing it?

Here is the full code to test:

<html>
<body>

  <div id="x"></div>

  <script src="http://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript">

    $(document).ready(function(){
      getVideos('cats');
    });

    function getVideos(query){
      var url = 'https://www.googleapis./youtube/v3/search?part=snippet&maxResults=2&q='+query+'&regionCode=us&type=video&fields=items(id)&key=AIzaSyCCOnozV0lEfnjfMTjpc4TFExAeIGJ6Fh0';
      $.ajax({
        url: url,
        success: function(data){
          appendVideos(data);
        }
      });
    }

    function appendVideos(data){
      // works here
      console.log(data);

      // but not in $.each
      $.each(data.items, function(i, item){
        $('#x').append(item.id.videoId + '<br>');
      });
    }

  </script>

</body>
</html>
Share Improve this question edited Feb 24, 2013 at 23:12 userBG asked Feb 24, 2013 at 23:05 userBGuserBG 7,19010 gold badges33 silver badges39 bronze badges 1
  • Well, when I alert (data.items) I get undefined in FF. – Leeish Commented Feb 24, 2013 at 23:10
Add a ment  | 

4 Answers 4

Reset to default 5

I looks like the data is being passed into the appendVideos as a string and not an object like you think. I did a

for(i in data) {
    alert(i+'='+data[i]);
}

And it's acting like a string.

This fixed it:

$.ajax({
    url: url,
    dataType: 'JSON',
    success: function(data){
      appendVideos(data);
    }
  });
}

In addition to the other answers, why not just use getJSON?

var url = 'https://www.googleapis./youtube/v3/search?part=snippet&maxResults=2&q='+query+'&regionCode=us&type=video&fields=items(id)&key=AIzaSyCCOnozV0lEfnjfMTjpc4TFExAeIGJ6Fh0';
$.getJSON(url, function(data) 
{
    appendVideos(data);
});

There error is found on line 622 of jQuery 1.9.1 It is caused by the Object that you are passing is being undefined. Line 622 makes a call to obj.length and since undefined has no property called length it throws and error.

Adding dataType: 'JSON' to your $.ajax request will fix this error

It looks like jQuery isn't automatically parsing the response as json in Firefox, rather it's interpreting it as a string.

If you change your success function to the following it should work:

success: function(data){
    if(typeof(data) === 'string') {
        data = JSON.parse(data);
    }

    appendVideos(data);
}

本文标签: javascripteach raises error (e is undefined) in Firefox but works fine in ChromeStack Overflow