jQuery 自动完成不会显示来自 PHP 的 JSON 响应

发布于 2024-11-17 15:20:57 字数 1811 浏览 0 评论 0 原文

我被 jQuery 自动完成的实现所困扰。我正在使用本教程: http://1300grams.com/2009/08/17/jquery-autocomplete-with-json-jsonp-support-and-overriding-the-default-search-parameter-q/

我的 jQuery 代码是这样的:

$("#quickSearchQuery").autocomplete("http://mydomain.net/json.php", {
    dataType: 'json',
    selectFirst: true,
    minChars: 2,
    max: 8,
    parse: function(data) {
        var rows = new Array();
        data = data.members;
        for(var i = 0; i < data.length; i++) {
            rows[i] = { id : data[i].id, name : data[i].name, location : data[i].location };
        }
        return rows;
    },
    formatItem: function(row, i, n) {
        return row.name + ' (' + row.location + ')';
    }
});

调用 http://mydomain.net/json.php?q=Alb 生成如下 JSON 响应:

{"query":"Alb","members":[{"id":"1","name":"Peter Albert","location":"New York"},{"id":"4","name":"Adalbert Smith","location":"Alabama"},{"id":"42","name":"Albert Einstein","location":"Vienna"}]}

我的问题是:它不起作用。自动完成不会出现,也不会创建任何元素。 如果我运行教程中的代码,一切都很好,并且我会得到自动完成的城市列表。但即使我只更改源 URI 并从 data.geonames 更改为 data.members (依此类推),自动完成程序也会停止工作。

我尝试过的方法:

  • 我将 dataType 更改为 jsonpjson,但 jsonp 创建了“无效标签” JSON 文件上的错误
  • 我用 header("Content-Type: application/json"); 修改了 PHP 脚本的内容类型
  • 我尝试了所有其他自动完成程序,但没有任何改变
  • 我尝试了所有修复从 Stack Overflow 的回复和教程中发现 。
  • 我将来自 geonames.org 的 JSON 响应复制到一个文件中,将其上传到我的服务器,并使用此硬编码响应作为查找源 仍然没有显示自动完成器:(

也许你有主意?

谢谢! 乔

I'm stuck with the implementation of the jQuery Autocomplete. I'm using this tutorial: http://1300grams.com/2009/08/17/jquery-autocomplete-with-json-jsonp-support-and-overriding-the-default-search-parameter-q/

My jQuery code is this:

$("#quickSearchQuery").autocomplete("http://mydomain.net/json.php", {
    dataType: 'json',
    selectFirst: true,
    minChars: 2,
    max: 8,
    parse: function(data) {
        var rows = new Array();
        data = data.members;
        for(var i = 0; i < data.length; i++) {
            rows[i] = { id : data[i].id, name : data[i].name, location : data[i].location };
        }
        return rows;
    },
    formatItem: function(row, i, n) {
        return row.name + ' (' + row.location + ')';
    }
});

A call to http://mydomain.net/json.php?q=Alb produces a JSON response like this:

{"query":"Alb","members":[{"id":"1","name":"Peter Albert","location":"New York"},{"id":"4","name":"Adalbert Smith","location":"Alabama"},{"id":"42","name":"Albert Einstein","location":"Vienna"}]}

My problem is: It just doesn't work. The Autocomplete doesn't appear, no elements are created.
If I run the code from the tutorial, everything is fine and I get the autocompleted list of cities. But even if I only change the source URI and from data.geonames to data.members (and so on), the autocompleter stops working.

What I've tried:

  • I changed dataType to jsonp and json, but jsonp creates the 'invalid label' error on the JSON file
  • I modified the PHP script's content-type with header("Content-Type: application/json");
  • I tried all the other Autocompleters, but nothing changed
  • I tried all the fixes I have found from responses at Stack Overflow and tutorials in the web
  • I copied a JSON response from geonames.org into a file, uploaded it to my server and used this hardcoded response as lookup source. Still no autocompleter showing :(

Maybe you got an idea?

Thanks!
Joe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-11-24 15:20:57

使用自动完成功能,您必须使用属性 id 和值才能使其正常工作。仅使用这些属性来呈现列表。

在你的 php 对象中,声明类似这样的内容(这个来自 C#,但认为你理解它):

public class Product
    {
        public String id { get; set; } //must be used
        public String value { get; set; } //must be used
        public String marque { get; set; }
    }

并将 json 序列化字符串返回给客户端。

json 是这样的,

[{"id":"1","value":"UMTS","comment":"umts comment"},
{"id":"2","value":"RAN","comment":"ran comment"},
{"id":"3","value":"Swap","comment":"swap comment"}]

它会在你的 JS 中自动映射,并且自动完成功能现在应该可以工作。

with autocomplete, you must use the property id and value to make it working. Only those properties are used to render the list.

In your php object, declare something like this (this one comes from C#, but think that you understand it):

public class Product
    {
        public String id { get; set; } //must be used
        public String value { get; set; } //must be used
        public String marque { get; set; }
    }

and return the json-serialized string to the client.

The json is something like that

[{"id":"1","value":"UMTS","comment":"umts comment"},
{"id":"2","value":"RAN","comment":"ran comment"},
{"id":"3","value":"Swap","comment":"swap comment"}]

It will be auto-mapped in your JS and the autocomplete should work for now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文