错误:Javascript 上的 [object Object]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10156309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: [object Object] on Javascript
提问by LE12
I keep getting an error when I run the javascript below in Firebug. I've tried changing multiple things and it still outputs the error. I am working with an api to retrieve information from the XML and then output it onto the screen but I keep getting an object error. Can someone see why?? Any help is appreciated!
当我在 Firebug 中运行下面的 javascript 时,我不断收到错误消息。我已经尝试更改多项内容,但它仍然输出错误。我正在使用 api 从 XML 中检索信息,然后将其输出到屏幕上,但我不断收到对象错误。有人能看出为什么吗??任何帮助表示赞赏!
$(document).ready(function() {
$('#searchbtn').bind('click' || 'enter',function(e) {
if ($.trim($('#searchBox').val()) !== '') {
$('#videos').append('<img src="img/loading.gif" alt="loading" class="loading" />');
getVideos(e);
}
});
});
function getVideos(e) {
e.preventDefault();
var text = 'text='+$('#searchBox').val();
$.ajax({
url: 'getVideos.php',
dataType: 'xml',
type: 'POST',
data: text,
success: function(data) {
$('#videos').append("<h1>The following events match your search!</h1>");
var xmlString = data;
if ($(xmlString).find('feed').children('entry').length == 0) {
$('#videos').append('<p class="noResults">Sorry, no results for you! Try searching again!</p>');
} else {
var videoTitle = [];
$(xmlString).find('title').each(function()
{
videoTitle.push($(this).text()) });
$('#videos').append('<ul>');
$(xmlString).find('entry').each(function(i) {
if (i == '40') {
return(false);
}
var vidInfo = '';
vidInfo += "<p>"+videoTitle[i]+"</p>";
$('#videos ul').append('<li>'+vidInfo+'</li>');
});
}
},
error: function(data) {
console.log('Error: ' + data);
}
})
};
回答by Chuck
When you append an object to a string, it gets its toString
method called, which for a plain object just gives the infamous "[object Object]". To log an object, you should just pass it straight into the console.log
function as an argument, like so:
当您将一个对象附加到一个字符串时,它会toString
调用它的方法,对于普通对象,它只会给出臭名昭著的“[object Object]”。要记录一个对象,您应该将它console.log
作为参数直接传递给函数,如下所示:
console.log('Error:', data);