jQuery的自动完成使用Ajax不会解析 [英] jQuery Autocomplete using Ajax won't parse

查看:112
本文介绍了jQuery的自动完成使用Ajax不会解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个基本的自动完成与阿贾克斯。我有结果的意义上制造麻烦。我是比较新的jQuery的,所以我对我的语法很抱歉,我在PHP更好。

I'm trying to use a basic autocomplete with ajax. I'm having trouble making sense of the results. I'm relatively new to jQuery so I apologize for my syntax, I'm better at PHP.

$("#category_title").autocomplete({
  source: function (request, response) {
    $.ajax({
       url: 'index.php?controller=AdminEvents&action=AutoComplete&variable=asdf',
       type: 'GET',
       success: function(data){
         response(data);
       }
    });
  },
  minLength: 2
});

从控制器的响应是样本数据,实际上并没有得到一个数据库做任何事:

The response from the controller is sample data, doesn't actually get anything from a database yet:

if ($this->isXHR())
{
  //$response = "{value1:test, value2:test2}";
  $response['value1'] = "test";
  $response['value2'] = "test2";
  $json = json_encode($response);
  print($json);
}

这里的一部分是怪我。基本上,这个工程和自动完成框弹出,但这里是它的回报:

Here's the part that's strange to me.. basically, this works and the autocomplete box pops up, but here's what it does with the return:

为什么?

感谢您的时间!

推荐答案

可能这可以帮助你。

的jQuery

$("#zipsearch").autocomplete({
                    source: function(req,res) {
                        $.ajax({
                            url: "index.php?controller=AdminEvents&action=AutoComplete&variable=asdf",
                            dataType: "json",
                            type: "GET",
                            data: {
                                term: req.term
                            },
                            success: function(data) {
                                res($.map(data, function(item) {
                                    return {
                                        label: item.value1,
                                        value: item.value1
                                    };
                                }));
                            },
                            error: function(xhr) {
                                alert(xhr.status + ' : ' + xhr.statusText);
                            }
                        });
                    }
                });

PHP

<?php
$response=array();
$response[0]['value1'] = "test";
$response[1]['value1'] = "test2";
print json_encode($response);
?>

这篇关于jQuery的自动完成使用Ajax不会解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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