jQuery 插件未从 AJAX 返回的 JSON 返回结果
所以我使用这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该插件可能有其优点,但作者似乎对 AJAX 的工作方式存在严重误解。这里的代码:
似乎对我来说假设“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:
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.
它的工作需要所有这三个属性。通过直接向源属性传递一个数组而不是 url 来验证它是否有效,然后尝试该 url。
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.