javascript 获取 TypeError:使用 ajax 获取数据时无效的“in”操作数 obj
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18460368/
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
Getting TypeError: invalid 'in' operand obj while fetching data using ajax
提问by user2621586
Below is my ajax call
下面是我的ajax调用
$(document).ready(function() {
$("#blog").focusout(function() {
alert('Focus out event call');
alert('hello');
$.ajax({
url: '/homes',
method: 'POST',
data: 'blog=' + $('#blog').val(),
success: function(result) {
$.each(result, function(key, val) {
$("#result").append('<div><label>' + val.description + '</label></div>');
});
},
error: function() {
alert('failure.');
}
});
});
});
I am getting 'TypeError: invalid 'in' operand obj ' error in my console
我在控制台中收到“TypeError: invalid 'in' 操作数 obj ' 错误
In advance thank you
提前谢谢你
回答by Swapnil Patil
Mention a dataType attribute in your ajax call.It consider text by default.That's why not able to iterate on result
在 ajax 调用中提及 dataType 属性。默认情况下它考虑文本。这就是为什么不能迭代结果
dataType:'json'
Because your resultshould be array or json
因为你的结果应该是数组或 json
回答by Manu M
the 'result' in success function should be an array
成功函数中的“结果”应该是一个数组
回答by Alex
Shouldn't data
be an object?
不data
应该是对象吗?
data: {
blog: $('#blog').val()
},