【发布时间】:2014-03-15 03:18:06
【问题描述】:
我是 JQuery 的新手,试图从 ColdFusion 查询中提取远程数据以显示在我的文本框中。
调用我的 CFC 时,我在 firebug 中收到错误:
未捕获的类型错误:无法使用“in”运算符在 in 中搜索“453”
我不知道这可能意味着什么。我可以看到这是从我的数据库中提取数据,因为在控制台输出的更下方,我看到了我的数据:
[{"value":1,"label":"Test article"}] jquery-1.10.2.js:997
谁能帮助解决这个错误?
完整的 HTML 代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!---Autocomplete Script --->
<link href="css/jquery-ui-1.10.4.custom.css" rel="stylesheet" type="text/css">
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/jquery-ui-1.10.4.custom.js"></script>
<!---<script src="Scripts/auto_correct.js" type="text/javascript"></script> --->
<script>
$(function() {
$( "#searchField" ).autocomplete({
source: function(request, response){
$.ajax({
url: "kb/cfcs/search.cfc?method=lookupTitle&returnformat=json",
dataType: "json",
data: {
searchterm: request.term
},
success: function(data){
response(data);
}
})
},
minLength: 3,
select: function(event, ui) {
$('#searchField').val(ui.item.value);
$('#defaultArticleID').val(ui.item.label);
}
});
});
</script>
</head>
<body class="oneColFixCtrHdr">
...
<!--- Main Container --->
<div id="container">
<cfform name="doesntmatter" method="post">
<label for="searchField">Search KB Articles</label>
<cfinput type="text" id="searchField" name="searchField" size="30em;" value=""/>
<cfinput type="hidden" name="defaultArticleID" id="defaultArticleID" value="0" />
</cfform>
</div>
...
</body>
</html>
我的 CFC:
<cfcomponent>
<cffunction name="lookupTitle" access="remote" output="no"
hint="I return a list of titles" returnformat="JSON">
<cfargument name="searchterm" required="false" default="" />
<!--- Define variables --->
<cfset var returnArray =ArrayNew(1)>
<!--- Do search --->
<cfquery name="data" datasource="#datasource#">
SELECT ID, title
FROM kbArticles
WHERE title LIKE <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.searchterm#%" />
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset titleStruct = structNew() />
<cfset titleStruct['value'] = ID />
<cfset titleStruct['label'] = title />
<cfset arrayAppend(returnArray,titleStruct) />
</cfloop>
<!--- And return it --->
<cfreturn returnArray />
</cffunction>
</cfcomponent>
更新
进行建议的更改时,我的自动完成表单字段中仍然没有收到任何数据。如果我在我的 CFADMIN 控制台中启用**启用请求调试输出**,我会看到这个错误:
未捕获的类型错误:无法读取未定义的属性“文档”。
当我单击控制台输出中错误旁边的链接时,它会显示以下内容:
function writeToWindow( win ) {
if( document.getElementById ) { // NS6
// failing on <table ... 100%> for unescape() ?, and failing on writeCSS without unescape(), no the issue is with ns6 writing out the <link> tag for css
// NS6 needs unescape() or else it writes 'showHide%28%27cf_debug_parameters%27,%27img_cf_debug_parameters%27%29;' for methods
//win.document.write(unescape(document.getElementById("cf_debug").innerHTML));
//NS6.2 wants it escaped
win.document.write(document.getElementById("cf_debug").innerHTML);
} else {
win.document.write(document.all['cf_debug'].innerHTML);
}
win.document.close();
win.focus();
}
【问题讨论】:
-
尝试将
<CFFORM>和<CFINPUT>标签更改为常规 HTML<FORM>和<INPUT>标签。您没有使用这些 ColdFusion UI 标签可以提供的任何额外功能,它们可能会破坏您的自定义 JavaScript。 -
你先搜索档案了吗?一个非常快速的搜索为该错误找到了几个线程:stackoverflow.com/questions/18502101/…。此外,在 both URL 和 cffunction 定义中指定
&returnformat=json可能会导致问题。尝试从函数定义中删除它。通过在浏览器中调用方法来测试它http://yourserver.com/kb/cfcs/search.cfc?method=lookupTitle&returnformat=json&searchterm={somethinghere} -
好的,所以将我的表单更改为 HTML 类型并从我的函数定义中删除 returntype="JSON" 没有任何效果。当我直接浏览我的 CFC 时,我看到我的数据输出如下: [{"value":1,"label":"Test article"}]
标签: jquery coldfusion