【发布时间】:2017-10-13 07:56:35
【问题描述】:
我在 jquery 中收到了这个错误:
Unhandled exception at line 422, column 4 in http://localhost:59307/lib/jquery/dist/jquery.js
0x800a138f - JavaScript runtime error: Unable to get property 'length' of undefined or null reference occurred
这显示在这个 jquery 方法中:
grep: function( elems, callback, invert ) {
var callbackInverse,
matches = [],
i = 0,
length = elems.length,
callbackExpect = !invert;
// Go through the array, only saving the items
// that pass the validator function
for ( ; i < length; i++ ) {
callbackInverse = !callback( elems[ i ], i );
if ( callbackInverse !== callbackExpect ) {
matches.push( elems[ i ] );
}
}
return matches;
},
具体是一行:
length = elems.length
这是由该 Ajax 语句引起的,该语句试图在 wwwroot 的 json 文件夹中查找 json 文件:
function GetBucklingData() {
var returnData;
$.ajax({
url: "/json/BucklingData.json",
async: false
}).done(function (data) {
returnData = JSON.parse(data);
});
return returnData;
}
这是一个函数中的示例 grep 语句:
var rods = $.grep(bucklingData, function (s) {
return (s.Bore == boreValue);
});
语句是这样调用的:
var bucklingData = GetBucklingData();
PopulateBoreDropdown(bucklingData);
似乎 ajax 无法找到 json 文件。该代码在 .net 之外运行,带有一个简单的 html 页面和一个独立文件夹,因此 json 文件看起来很好。
有什么想法吗?
【问题讨论】:
-
这将失败,因为回调“完成”中的 returnData 分配发生在
return returnData指令之后 -
你能说明如何调用 grep 吗?
-
问题已更新
标签: javascript jquery .net ajax