使用Ajax的jQuery Autocomplete将无法解析
我正在尝试使用ajax的基本自动完成function。 我无法理解结果。 我对jQuery比较新,所以我为我的语法道歉,我更擅长PHP。
$("#category_title").autocomplete({ source: function (request, response) { $.ajax({ url: 'index.php?controller=AdminEvents&action=AutoComplete&variable=asdf', type: 'GET', success: function(data){ response(data); } }); }, minLength: 2 });
控制器的响应是样本数据,实际上并没有从数据库中获取任何内容:
if ($this->isXHR()) { //$response = "{value1:test, value2:test2}"; $response['value1'] = "test"; $response['value2'] = "test2"; $json = json_encode($response); print($json); }
这是对我来说很奇怪的部分..基本上,这是有效的,弹出自动完成框,但这是它对返回的作用:
为什么?
谢谢你的时间!
试试这个:
jQuery的
$(document).ready(function(){ $('#zipsearch').autocomplete({source:'suggest_zip.php', minLength:2}); });
PHP
$response = array(); $response[0]=array('label'=>'test','value'=>'test'); $response[1]=array('label'=>'test2','value'=>'test2'); echo json_encode($response);
可能这对你有所帮助
jQuery的
$("#zipsearch").autocomplete({ source: function(req,res) { $.ajax({ url: "index.php?controller=AdminEvents&action=AutoComplete&variable=asdf", dataType: "json", type: "GET", data: { term: req.term }, success: function(data) { res($.map(data, function(item) { return { label: item.value1, value: item.value1 }; })); }, error: function(xhr) { alert(xhr.status + ' : ' + xhr.statusText); } }); } });
PHP