2
我試圖與Typeahead.js從它返回一個簡單的JSON字符串的數據ColdFusion組件獲取其數據的簡單測試。引導預輸入來電的ColdFusion
這裏是我的AjaxCtrl.cfc
<cfcomponent output="false">
<cffunction name="GetParams" access="remote" output="false">
<cfset objData = [
{ PARAMCODE = "SYSTEM_PARAM_1",
PARAMVAL = "FALSE"
},
{ PARAMCODE = "SYSTEM_PARAM_2",
PARAMVAL = true
},
{ PARAMCODE = "SYSTEM_PARAM3",
PARAMVAL = "1003"
},
{ PARAMCODE = "SYSTEM_PARAM4",
PARAMVAL = 1004
}
] />
<cfreturn objData >
</cffunction>
</cfcomponent>
所以,當我嘗試訪問http://localhost/foo/bar/AjaxCtrl.cfc?method=GetParams
在瀏覽器中我得到預期的輸出如下:
[{"PARAMCODE":"SYSTEM_PARAM_1","PARAMVAL":false},{"PARAMCODE":"SYSTEM_PARAM_2","PARAMVAL":true},{"PARAMCODE":"SYSTEM_PARAM_3","PARAMVAL":1003},{"PARAMCODE":"SYSTEM_PARAM_4","PARAMVAL":1004}]
那麼這裏就是我的JavaScript文件
// constructs the suggestion engine
var engine = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.PARAMCODE); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: "http://localhost/dev/test/djb/AjaxCtrl.cfc?method=GetParams"
}
);
// kicks off the loading/processing of `local` and `prefetch`
engine.initialize();
$("input.typeahead").typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'parameters',
displayKey: 'PARAMCODE',
source: engine.ttAdapter()
});
到目前爲止,我只能使它與本地硬編碼da ta例子。我試過的所有例子都不適用於預取或遠程。我究竟做錯了什麼?
不要忘記刪除serializeJSON。當你使用returnformat = json時,CF會爲你序列化它。另外,您不必將其添加到該功能中。你也可以通過URL傳遞它。 iee some.cfc?method = something&returnformat = json。 –
@RaymondCamden這是真的!我從工作代碼中刪除了它,但忘了在這裏提及,謝謝! – dominicbri7