自动完成请求/服务器响应是什么样的? [英] What does autocomplete request/server response look like?

查看:34
本文介绍了自动完成请求/服务器响应是什么样的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个黑洞:在 jQuery UI 网站上搜索了一个小时后,堆栈溢出和谷歌搜索,我还没有找到关于如何编写自动完成的服务器端的最基本信息.

传递给服务器的参数是什么,JSON 响应应该是什么样的?

我一定遗漏了一些东西,因为其他人是如何学会如何做到这一点的?网站似乎只讨论客户端 JavaScript 代码,从不讨论协议或服务器端示例.

我需要足够的东西来让最简单的远程示例工作.

解决方案

传递给服务器的参数是什么

您需要将 request.term 传递给您的服务器端代码(来自文档):

<块引用>

一个请求对象,有一个称为术语"的属性,指的是到当前文本中的值输入.

基本上,在您的 autocomplete 代码中,您将拥有如下内容:

$("#autocomplete").autocomplete({//request.term 需要传递给服务器.来源:函数(请求,响应){ ... }});

<块引用>

JSON 响应应该是什么样的喜欢吗?

autocomplete 小部件需要一个带有 labelvalue 属性的 JSON 对象数组(尽管如果您只指定 value,它将用作标签).所以在最简单的情况下,你可以只返回如下所示的数据:

<预><代码>[{标签:'C++',值:'C++'},{标签:'Java',值:'Java'}{ 标签:'COBOL',值:'COBOL' }]

如果您需要更复杂的东西,您可以使用 $.ajax 函数的 success 参数来规范化您在自动完成获取之前返回的数据:

源:函数(请求,响应){$.ajax({/* 截图 */成功:功能(数据){响应($.map(数据.地理名称,功能(项目){返回 {标签: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,值:item.name}}));}});

此代码取自 此处(这是一个很好的示例)ajax + 自动完成在更复杂的场景中工作).

基本上,在成功的 ajax 请求后,接收到的数据被标准化(使用 $.map)到自动完成小部件所期望的.

希望有所帮助.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆