什么是自动完成的请求/服务器响应是什么样子? [英] What does autocomplete request/server response look like?
问题描述
这似乎是一个黑洞:搜索 jQuery UI的网站的一个小时后,堆叠式NBSP;溢出,谷歌搜索,我还没有找到如何写自动完成的服务器端的最基本的信息。
被传递到服务器的参数是什么,什么应该JSON响应什么样子的?
我一定是失去了一些东西,因为怎么没其他人学习如何做到这一点?网站似乎只讨论客户端JavaScript code,从来没有协议或服务器端的例子。
我需要足以让最简单的遥控器为例工作。
时,传递给服务器什么参数
您需要传递 request.term
到你的服务器端code(从文件):
有一个请求对象,具有单 属性称为术语,这是指 到目前,在文本中的值 输入。
基本上,在你的自动完成
code,你就会有这样的事情:
$(#自动完成)。自动完成({
// request.term需要传递到服务器。
源:功能(请求,响应){...}
});
和我应该JSON响应一下 什么样的?
在自动完成
部件愿与标签
和值属性(但如果你只是指定
值
,它将被用作标签)。因此,在简单的情况下,你可以返回的数据是这样的:
[
{标签:C ++,值:C ++},
{标签:Java的,值:Java的'}
{标签:COBOL,值:COBOL}
]
如果你需要更复杂的东西,你可以使用成功
的 $。阿贾克斯
函数的参数正常化数据等你回来的自动完成得到它之前:
来源:功能(请求,响应){
$阿贾克斯({
/ *剪断* /
成功:功能(数据){
回应($。地图(data.geonames,函数(项目){
返回 {
标签:item.name +(item.adminName1,+ item.adminName1:?)+,+ item.countryName,
值:item.name
}
}));
}
});
这code取自例如这里(这是阿贾克斯的一个很好的例子整体+自动填充在更复杂的情况作品)。
基本上,这是怎么回事的是,在一个成功的Ajax请求,接收到的数据的正常化(使用 $。地图
)的自动完成构件所预期的。
希望有所帮助。
This seems to be a black hole: After an hour of searching the jQuery UI website, Stack Overflow, and googling, I've yet to find the most basic information of how to write the server side of the AutoComplete.
What parameter is passed to the server and what should the JSON response look like?
I must be missing something, because how did everyone else learn how to do this? Sites only seem to discuss the client-side JavaScript code and never the protocol or server-side examples.
I need enough to get the simplest remote example working.
What parameter is passed to the server
You need to pass request.term
to your server-side code (from the documentation):
A request object, with a single property called "term", which refers to the value currently in the text input.
Basically, in your autocomplete
code, you'll have something like this:
$("#autocomplete").autocomplete({
// request.term needs to be passed up to the server.
source: function(request, response) { ... }
});
and what should the JSON response look like?
The autocomplete
widget expects an array of JSON objects with label
and value
properties (although if you just specify value
, it will be used as the label). So in the simplest case, you can just return data that looks like this:
[
{ label: 'C++', value: 'C++' },
{ label: 'Java', value: 'Java' }
{ label: 'COBOL', value: 'COBOL' }
]
If you need something more complicated, you can use the success
argument of the $.ajax
function to normalize the data you get back before the autocomplete gets it:
source: function( request, response ) {
$.ajax({
/* Snip */
success: function(data) {
response($.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
This code is taken from the example here (This is a good example overall of ajax + autocomplete works in a more complex scenario).
Basically, what's going is that upon a successful ajax request, the data received is being normalized (using $.map
) to what the autocomplete widget expects.
Hope that helps.
这篇关于什么是自动完成的请求/服务器响应是什么样子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!