javascript - 未捕获的类型错误:无法读取 null 的属性“forEach”
问题描述
我在 ajax 响应中收到此错误:
$.ajax({
type: "GET",
url: "/search",
data: {
s: value,
},
success: function (data, textStatus, jqXHR) {
console.log('data is:', data);
if (data != null) {
jsonObject = JSON.parse(data);
jsonObject.forEach(function (data) { //error here
// do stuff
} else {
console.log('null data received');
}
但我总是得到这个错误:
data is: null
projects.js:23 Uncaught TypeError: Cannot read property 'forEach' of null
at Object.success (projects.js:23)
at c (jquery-3.5.1.min.js?1624781025:2)
at Object.fireWith [as resolveWith] (jquery-3.5.1.min.js?1624781025:2)
at l (jquery-3.5.1.min.js?1624781025:2)
at XMLHttpRequest.<anonymous> (jquery-3.5.1.min.js?1624781025:2)
我的查找空数据的条件有什么问题,我该如何解决?
解决方案
错误说jsonObject
是null
.
您正在测试 if data
is null
。
因此data
必须null
以 JSON 表示,字符串data
也是如此。 null
( "null" != null
)
推荐阅读
- apache-spark - Pyspark - 在 enableHiveSupport() 时创建增量表
- laravel - CloudFlare 无效的 SSL 证书
- javascript - MongoDB/Mongoose - 查找特定日期在日期范围内的所有位置
- c# - 如何计算一个字符串值列表并计算它在给定文件中重复的次数
- javascript - addEventListener 在第一次执行后停止
- r - 使用 nls 进行指数回归
- mongodb - 如何仅打印 MongoDB 中子嵌入文档中的值?
- java - 如何在连接期间忽略空字符串?
- windows - MacOS .app 文件显示和行为类似于 Windows 资源管理器中的文件夹
- oracle - ORA-00904: : oracle 中的无效标识符错误修复