javascript - 长度函数抛出未捕获的错误
问题描述
执行此代码时出现错误。该代码运行良好,但引发了未捕获的错误。我应该担心这个吗?
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response = JSON.parse(this.responseText);
}
document.getElementById(type).options.length = 0;
for (var i = 0; i < response.length ; i++) {
var option = document.createElement("OPTION");
option.innerHTML = response[i];
option.value = response[i];
ddlEnvType.options.add(option);
}
document.getElementById(type).disabled = false;
};
xmlhttp.open("POST","sample.php?condition=" +x.value +"&type=" +type,true);
xmlhttp.send();
}
解决方案
document.getElementById(type).options.length = 0;
上面的行抛出错误,因为.length
用于返回length
并且不能用于为其分配值。
在 for 循环中
i<response.length
但响应不是一个数组,它是一个 JSON 对象,这是导致错误的原因
response = JSON.parse(this.responseText);
推荐阅读
- reactjs - 在单独的 d3 图中连接点
- openshift - Openshift 3.11 + 503 响应
- testing - Ubuntu 18 自动完成错误?“bash:cd:参数太多”
- javascript - 如何在javascript中为以下快速排序算法设置动画?
- sqlite - 从 SQLite 数据库中选择满足限制条件的行,返回具有最小 ID 的行
- python - 如何使用 pandas 生成数据框
- javascript - Javascript跨域请求
- protocols - Torrent 握手响应大于 68 字节
- excel - 协调来自两个 Excel 工作表的数据
- jquery - Save the jQuery Ajax Response in global variable to send it to another page (no local or session storage)