javascript 未捕获的类型错误:无法使用“in”运算符在(jQuery)中搜索“0”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25123716/
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
Uncaught TypeError: Cannot use 'in' operator to search for '0' in (jQuery)
提问by Thalatta
I feel like this has to do with the AJAX call. Not really sure what is going on. The error is technically being thrown within the jQuery file at line 584 which defines the isArraylike(obj)
function.
我觉得这与 AJAX 调用有关。不太确定发生了什么。从技术上讲,错误是在定义isArraylike(obj)
函数的 jQuery 文件的第 584 行引发的。
jQuery(document).ready(function(){
var width_of_grams = $(window).width();
var new_pic_height = (width_of_grams /7);
$("img.gram_photo").css('height', (width_of_grams) / 7);
$("#instafeed").css('height', 2*new_pic_height);
$(window).resize(function() {
var width_of_grams = $(window).width();
var new_pic_height = (width_of_grams /7);
$("img.gram_photo").css('height', (width_of_grams) / 7);
$("#instafeed").css('height', 2*new_pic_height);
});
$("#school_application_fls_center_name").change(function(){
var center_name = document.getElementById("school_application_fls_center_name").value;
var formdata = {center: center_name};
$.ajax({
url: "/application/get_programs_for_center",
type: "POST",
data: formdata,
success: function(response){
var options = $("#school_application_program_name");
console.log(response);
$.each(response, function(i,item) {
options.append($("<option />").val(response[i].id).text(response[i].name));
});
}
});
});
});
Here is the jQuery library code that throws the error:
这是引发错误的 jQuery 库代码:
function isArraylike( obj ) {
var length = obj.length,
type = jQuery.type( obj );
if ( type === "function" || jQuery.isWindow( obj ) ) {
return false;
}
if ( obj.nodeType === 1 && length ) {
return true;
}
return type === "array" || length === 0 ||
typeof length === "number" && length > 0 && ( length - 1 ) in obj; //THIS LINE THROWS ERROR
}
N.B. When I remove all of the callback function in the success part of the AJAX request,
and just put a console.log
to confirm I am making it to the callback successfully it prints
my log message and does not throw the error.
注意,当我删除 AJAX 请求成功部分中的所有回调函数,并只输入一个console.log
以确认我成功进入回调时,它会打印我的日志消息并且不会抛出错误。
回答by sifriday
I think your response is a JSON string. Try passing
我认为您的响应是一个 JSON 字符串。试试通过
dataType: 'json'
in your ajax parameters, or explicitly deserialise it with:
在您的 ajax 参数中,或使用以下命令显式反序列化它:
response = JSON.parse(response)
before you call $.each.
在调用 $.each 之前。
Browser session:
浏览器会话:
// Create some JSON:
json = '["foo", "bar"]'
"["foo", "bar"]"
// Try parsing it, to check it is indeed JSON:
JSON.parse(json)
["foo", "bar"]
// But without parsing it, call $.each on it - we get your error:
$.each(json, function(idx, thing) {console.log(thing)})
TypeError: Cannot use 'in' operator to search for '13' in ["foo", "bar"]
// Now try $.each and JSON.parse - success!
$.each(JSON.parse(json), function(idx, thing) {console.log(thing)})
foo VM410:2
bar VM410:2
["foo", "bar"]
回答by Thalatta
So when I was getting this error, I was not actually correctly supplying the response on the server side. In my Rails Controller I had:
因此,当我收到此错误时,实际上并没有在服务器端正确提供响应。在我的 Rails 控制器中,我有:
def controller_method_to_handle_post
render nothing :true
end
So I basically had a null response coming from the server. So then when I went to parse
the response with $.each
I hit the jQuery error which is saying it can't iterate through this null object that I am passing it.
所以我基本上有一个来自服务器的空响应。所以当我去解析响应时,$.each
我遇到了 jQuery 错误,它说它无法遍历我传递的这个空对象。