php - 这个 jQuery UI 自动完成远程 JSON 请求 Google Suggest (Codeigniter) 有什么问题吗?

标签 php jquery-ui codeigniter

我正在尝试模仿 Google Suggest。我收到了 Google 的回复,所以一切都在那里工作,但由于某种原因我无法显示自动完成选项。

这是我的 Codeigniter Controller 函数:

public function google_suggest($query)
{
    //$query = implode(" ", $query);

    $url = 'http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en&q=' . urlencode($query);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
    $data = curl_exec($ch);
    curl_close($ch);

    if ($data)
    {
        $data = json_decode($data, true);
        $data = $data[1];

        foreach ($data as $word)
        {
            $row['id'] = $word;
            $row['value'] = $word;
            $row_set[] = $row;
        }
        var_dump($row_set); //this works and responds so I know there is data
        echo json_encode($row_set);
    }
    else
    {
        return NULL;
    }
}

示例输出:

[{"id":"testosterone","value":"testosterone"},{"id":"test","value":"test"},{"id":"test my speed","value":"test my speed"},{"id":"testicular cancer","value":"testicular cancer"},{"id":"testament","value":"testament"},{"id":"testosterone pills","value":"testosterone pills"},{"id":"testicular pain","value":"testicular pain"},{"id":"testudo","value":"testudo"},{"id":"testicular torsion","value":"testicular torsion"},{"id":"testosterone cypionate","value":"testosterone cypionate"}]

这是我的客户端 View :

<style>
.ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
</style>
<script>
$(function() {
    function log( message ) {
        $( "<div/>" ).text( message ).prependTo( "#log" );
        $( "#log" ).scrollTop( 0 );
    }

    $( "#birds" ).autocomplete({
        source: "<?=base_url()?>/index.php/search/google_suggest/", // this link definitely works as I've tried it in isolation in another AJAX function
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
    });
    });
    </script>

<div class="ui-widget">
    <label for="birds">Birds: </label>
    <input id="birds" />
</div>

最佳答案

尝试提醒(“/index.php/search/google_suggest/”);我相信 base_url 以斜杠结尾,在您的示例中可能如下所示:www.site.com//index.php/search/google_suggest/。

更糟糕的情况是,使用 Firebug 或 Chrome 开发者工具来查看问题所在。

关于php - 这个 jQuery UI 自动完成远程 JSON 请求 Google Suggest (Codeigniter) 有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805267/

相关文章:

php - 没有 bind_param 的 mysqli 准备语句

php - 使用 mysqldump 创建备份

javascript - jQuery datepicker 没有正确更新值

php - fatal error : Call to undefined function site_url()

mysql - 基于codeigniter中的数组从数据库中获取值

php - 系统和用户消息结构

php - 如何知道数据库中是否存在关系中的对象

javascript - jQuery Sort 后获取更新的订单

javascript - 错误 : missing : after property id

php - CodeIgniter,Ajax 调用未进入 Controller