douling0053 2017-02-16 20:19
浏览 28

Typeahead - 如何正确编码?

For a website I wanted to realize a type ahead search with typeahead.js from twitter to gather information from a local database. For the first step I was looking for some easy code which I found on a blog here.

My developing enviroment is a local XAMP stack on a Mac. I know all the basics about AJAX and created some good working stuff with simple javascript. I am new to jQuery and just copied the code from the blog website. I loaded down newest jQuery and typeahead.js framework and implemented it as the blog website recommends it. jQuery is initiated properly, but I cant control if typeahead.js is initiated properly. Bootstrap I left out at this step assuming that its not relevant for my test. I simply wanted to catch the Ajax call in PHP. But finally I have no success. This is the basic code.

<head>
    <meta http-equiv="Content-Language" content="en-us">
    <title>PHP MySQL Typeahead Autocomplete</title>
    <meta charset="utf-8">
        echo '<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>'; 
        echo '<script type="text/javascript" src="js/typeahead.jquery.min.js"></script>'; 

    <script>
        $(document).ready(function() {

            $('input.city').typeahead({
                name: 'city',
                remote: 'city.php?query=%QUERY'
            });

        })
    </script>
</head>

<body>
    <div class="content">

        <form>
            <h1>Try it yourself</h1>
            <input type="text" name="city" size="30" class="city" placeholder="Please Enter City or ZIP code">
        </form>
    </div>
</body>

</html>

And here is the city.php for for simple catching the AJAX call and sending back the query.

<?php

if (isset($_REQUEST['query'])) 
  echo json_encode(array('label' => 'success', 'value' => $_REQUEST['query'] '));
else
  echo json_encode(array('label' => 'no success', 'value' => $_REQUEST['query'] '));


?>

All the code should do here is showing that typeahead and AJAX works. But it doesnt. And I do not know why. So my questions are :

  1. Is the remote key in the jQuery function properly setup ?

  2. The php code uses the $_REQUEST variable, other code refer to $_GET. What is correct ?

  3. With javascript I can log to the console easily on the AJAX callback with console.log(...). How do that with jQuery ?

Any help is appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作