【发布时间】:2017-08-23 02:26:57
【问题描述】:
未捕获的类型错误:无法读取未定义的属性“toLowerCase”
at Object.<anonymous> (<anonymous>:16:76) at Function.each (jquery.js?v=1.4.4:34) at Object.success (<anonymous>:10:13) at Function.handleSuccess (jquery.js?v=1.4.4:143) at XMLHttpRequest.w.onreadystatechange (jquery.js?v=1.4.4:142)
在以下代码的注释掉的 if 代码中尝试使用 toLowerCase() 函数时,我不断收到上述错误:
(function($) {
$.ajax({
url: "/node.json",
data: {'type': "resource_page", 'page': 3},
dataType: "json",
type: "GET",
success: function (response) {
$.each(response.list, function(k, resource) {
var title = resource.title;
var description = resource.body.value;
if(title.toLowerCase().indexOf("biology") >= 0) { console.log(description.toLowerCase().indexOf("biology")); };
//if(title.toLowerCase().indexOf("biology") >= 0 || description.toLowerCase().indexOf("biology") >=0) { console.log(title); };
});
}
});
}(jQuery));
当我 consol.log() 一切正常(我得到 226 的结果,说“生物学”确实出现在字符串中)。我什至可以说:
if(title.toLowerCase().indexOf("biology") >= 0) { console.log(description.toLowerCase()); };
并且仍然得到<p>this video database is designed to teach laboratory fundamentals through simple, easy to understand video demonstrations. this collection demonstrates how to execute basic techniques commonly used in cellular and molecular biology. to enhance your understanding of the methods each video is paired with additional video resources to show practical applications of the techniques and other complementary skills.</p>的正确console.log
我不确定为什么我会收到描述错误,而不是标题错误。在 if 中使用它之前,我曾尝试在描述中使用 toLowerCase() ,但这不起作用。我只能在 console.logging 时使用它。谁能帮帮我。非常非常感谢!
【问题讨论】:
-
错误表示
description是undefined。
标签: javascript jquery ajax