Javascript Jquery中的Ajax自动完成无法显示结果
我有以下代码:Javascript Jquery中的Ajax自动完成无法显示结果,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有以下代码: $(document).ready(function() { src = '<?php echo admin_url( 'admin-ajax.php' ); ?>'; // Load the cities straight from the server, passing the country as an extra param $("#k").autocomplete({ source: f
$(document).ready(function() {
src = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
// Load the cities straight from the server, passing the country as an extra param
$("#k").autocomplete({
source: function(request, response) {
$.ajax({
url: src,
dataType: "json",
data: {
k : request.term,
dict : $("#dict").val(),
action :"autocomplete_dict"
},
success: function (data) {
response($.map(data, function (item) {
return item.value;
}))
}
});
},
// optional (if other layers overlap autocomplete list)
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000);
$(".ui-autocomplete-loading").css("background:url",'img/indicator.gif no-repeat right center');
},
min_length: 3
});
});
我可以看到从浏览器到服务器的请求,但它无法显示服务器的结果。有人能帮我。我做错了什么?您不能返回成功函数中的项目 试试看:
...
success: function( data ) {
response(data);
}
这是可以工作的 我要试试看。您的问题在刚接触jQuery的开发人员中很常见。$。ajax是一个异步函数,这意味着它是一个非阻塞任务。函数本身在返回任何数据之前很久就完成了。我认为你必须做的是计时,这样你就知道数据参数中有一些东西
$.ajax({
url: src,
dataType: "json",
data: {
k : request.term,
dict : $("#dict").val(),
action :"autocomplete_dict"
},
success: function (data) {
$("#K").autocomplete({
Source:data // not sure what Response is but you may need to Response data if its a transformer of some sort.
})
}))
}
});
这将首先触发ajax,然后在请求完成后立即启动autocomplete,其中包含真实数据。$。map返回的是数据,而不是成功function@quynt您的问题可能在响应函数中。把它贴上去。你试过console.log(数据)吗?我不知道这是否是你遇到的问题,但是你也没有json.parse(数据),所以你只是映射了一个字符串,我在这篇文章中尝试过:但这也不起作用。
$.ajax({
url: src,
dataType: "json",
data: {
k : request.term,
dict : $("#dict").val(),
action :"autocomplete_dict"
},
success: function (data) {
$("#K").autocomplete({
Source:data // not sure what Response is but you may need to Response data if its a transformer of some sort.
})
}))
}
});