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+'®ionCode=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+'®ionCode=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
4 Answers
Reset to default 5I 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+'®ionCode=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
版权声明:本文标题:javascript - $.each raises error (e is undefined) in Firefox but works fine in Chrome - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744177384a2594065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论