Twitter的阿贾克斯事先键入的内容产生不确定不确定、内容、Twitter、阿贾克斯

2023-09-10 19:26:39 作者:照亮沉浮

我建设与Twitter预输入一个自动完成使用AJAX JSON调用我的PHP文件中得到一些数据,但它一直显示在下拉结果列表如下:

I'm building a autocomplete with twitter typeahead using a ajax JSON call to my php file to get some data but it keeps displaying the following in the dropdown result list:

未定义

未定义

未定义

但是当我做的:

alert(data); 

我得到显示正确的数据,但不知何故自动完成列表中继续显示不确定的,我已经阅读并试图多件事情的一些文章在这里的计算器,但我似乎无法得到它的工作。

i get the right data displayed but somehow the autocomplete list keeps displaying undefined, ive read and tried multiple things by some articles here on stackoverflow, but i can't seem to get it to work.

我要下的jQuery code:

I have to following jquery code:

      $('.item-name .typeahead').typeahead(null,{
      source: function (query, process) {
        $.ajax({
          url: 'ajaxItems.php',
          type: 'POST',
          dataType: 'JSON',
          data: 'query=' + query,
          success: function(data) {
            // alert(data);
            process(data);
          }
        });
      }
    });

和我ajaxItems.php具有以下code测试目的:

And my ajaxItems.php has the following code for testing purpose:

<?PHP
$results = array();

$results[] = 'jeans';
$results[] = 'sweater';

$json =  json_encode($results);
print_r($json);
?>

的JSON输出如下:

The JSON output is as follows:

["jeans","sweater"]

我希望有人能照到什么即时通讯一些轻做错了,或点我在正确的方向。在先进的感谢!

I hope someone can shine some light on what im doing wrong or point me in the right direction. Thanks in advanced!

修改的 我使用下面的预输入文件: http://twitter.github.io/typeahead.js/releases /latest/typeahead.bundle.js

edit I am using the following typeahead file: http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js

推荐答案

我不得不使用来源类似的问题:和我结束了使用远程: 。 对你来说,这将是这样的:

I had similar problems using source: and I ended up using remote:. In your case, it would be something like this:

$('.item-name .typeahead').typeahead({
    remote: 'ajaxItems.php?query=%QUERY'
  });

请注意,我删除了预输入(空,{,因为我认为这是没有必要的,但我可能是错误的。很明显,你将不得不使用 $ _ GET 而不是 $ _ POST ,但我认为这是很容易通过这种方式

Note that I deleted null on typeahead(null,{ since I think it's not necessary but I might be wrong. Obviously, you'll have to use $_GET instead of $_POST but I think it's much easier this way.