jQuery 插件未从 AJAX 返回的 JSON 返回结果

发布于 2024-10-31 18:26:49 字数 1844 浏览 3 评论 0原文

所以我使用这个 jQuery 插件: jSuggest
这是基于这个插件的: autoSuggest

这是一个带有(工作)演示的 jsFiddle jSuggest 的: demo

这是我用来在我的页面上实例化插件的代码:

<form id="add" action="components/AddItem.php" method="post" enctype="multipart/form-data" class="center clear">
            <fieldset>
                <legend>Basic Information</legend>
                <label for="name">Name</label>
                <br />
                <input type="text" name="name" id="name"/>
                ...[snip]...
        </form>

$( '#name' ).jSuggest({
                        source: "components/suggItem.php",
                        selectedItemProp: "name",
                        seekVal: "name",
                        selectionLimit: 1,
                        uniqID: "item",
                        keyDelay: 100,
                        newText: "You must click outside the text box to add a new item."
                        });

这是当我在文本框中输入“ch”时从 "components/suggItem.php" 返回的字符串:

[ {"value":"1","name":"Cheeseburger "},{"value":"3","name":"Fish Sandwich"} ]

(这是 Content-type: application/json 我从 FireBug 获取它)

但是,我曾经在下拉列表中得到的唯一内容是“未找到结果”。任何人都可以找到我的代码中的错误吗?

我也尝试过:

$( '#name' ).jSuggest({
                        source: "components/suggItem.php",
                        seekVal: "name",
                        });

以及“value”“name”的各种组合。

我不明白为什么这不起作用。有什么帮助吗?

So I'm using this jQuery plugin:
jSuggest
Which is based on this plugin: autoSuggest

Here's a jsFiddle with the (working) demo of jSuggest: demo

This is the code I am using to instantiate the plugin on my page:

<form id="add" action="components/AddItem.php" method="post" enctype="multipart/form-data" class="center clear">
            <fieldset>
                <legend>Basic Information</legend>
                <label for="name">Name</label>
                <br />
                <input type="text" name="name" id="name"/>
                ...[snip]...
        </form>

.

$( '#name' ).jSuggest({
                        source: "components/suggItem.php",
                        selectedItemProp: "name",
                        seekVal: "name",
                        selectionLimit: 1,
                        uniqID: "item",
                        keyDelay: 100,
                        newText: "You must click outside the text box to add a new item."
                        });

This is the string that returns from "components/suggItem.php" when I type "ch" into the textbox:

[ {"value":"1","name":"Cheeseburger"},{"value":"3","name":"Fish Sandwich"} ]

(That's Content-type: application/json and I'm getting it from FireBug)

However, the only thing I ever get in the dropdown is "No Results Found". Can anyone find a bug in my code?

I have also tried:

$( '#name' ).jSuggest({
                        source: "components/suggItem.php",
                        seekVal: "name",
                        });

and various combinations of "value" and "name".

I cannot figure out why this doesn't work. Any help?

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

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

发布评论

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

评论(2

抽个烟儿 2024-11-07 18:26:49

该插件可能有其优点,但作者似乎对 AJAX 的工作方式存在严重误解。这里的代码:

        // If the data is a URL, retrieve the results from it. Else, the data is an object, retrieve the results directly from the source.
        if (dType === 'string'){

          // Set up the limit of the query.
          var limit = qLimit ? "&limit="+encodeURIComponent(qLimit) : '';

          // Build the query and retrieve the response in JSON format.
          $.getJSON(theData+"?"+opts.queryParam+"="+encodeURIComponent(string)+limit+opts.extraParams, function(rData){ theData = rData; });

        }

        // Call the custom retrieveComplete function.
        theData = opts.retrieveComplete.call(this, theData);

似乎对我来说假设“getJSON”调用将同步调用函数参数,这样变量“theData”将在执行最后一行(显示的)代码之前更新。然而,事实并非如此,“theData”是驱动整个自动完成机制的关键对象。

原始代码(“autoSuggest”插件似乎是“jSuggest”的前身)看起来与该代码截然不同,并且它正确地将 ajax 调用返回的 JSON 数据的解释推迟到处理程序例程。

That plugin may have its advantages, but the author appears to be suffering from a significant misunderstanding of the way AJAX works. This code here:

        // If the data is a URL, retrieve the results from it. Else, the data is an object, retrieve the results directly from the source.
        if (dType === 'string'){

          // Set up the limit of the query.
          var limit = qLimit ? "&limit="+encodeURIComponent(qLimit) : '';

          // Build the query and retrieve the response in JSON format.
          $.getJSON(theData+"?"+opts.queryParam+"="+encodeURIComponent(string)+limit+opts.extraParams, function(rData){ theData = rData; });

        }

        // Call the custom retrieveComplete function.
        theData = opts.retrieveComplete.call(this, theData);

appears to me to assume that the "getJSON" call will invoke the function argument synchronously, such that the variable "theData" will be updated before that last (shown) line of code is executed. That's just not going to be true, however, and "theData" is the key object that drives the whole autocomplete mechanism.

The original code (for the "autoSuggest" plugin that seems to be the progenitor of "jSuggest") looks critically different around that code, and it correctly defers the interpretation of the JSON data returned from the ajax call to the handler routine.

转身以后 2024-11-07 18:26:49
  $( '#name' ).jSuggest({source: "components/suggItem.php",                              
selectedItemProp: "name", 
seekVal: "name"  
 });

它的工作需要所有这三个属性。通过直接向源属性传递一个数组而不是 url 来验证它是否有效,然后尝试该 url。

  $( '#name' ).jSuggest({source: "components/suggItem.php",                              
selectedItemProp: "name", 
seekVal: "name"  
 });

All these three properties are required for it to work. Verify that it works by passing the source property an array directly rather than the url, and once that is going try the url.

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