代码之家  ›  专栏  ›  技术社区  ›  Abhishek Patel

jQuery TokenInput无法与laravel一起工作(显示搜索结果)

  •  0
  • Abhishek Patel  · 技术社区  · 10 年前

    这是我在页面中使用的javascript代码。

    <script>
        $(document).ready(function() {
            $("#tokenfield").tokenInput("localhost/inkrasa3/public/hashes",
                {theme:"facebook",tokenLimit:5,preventDuplicates:true,tokenValue:"name"});
        });
    </script>
    

    这是路线文件。

    Route::get('hashes',function(){
    return "[{id: 1, name:\"hello\"},{id:2, name:\"sup\"}]";
    });
    

    我做错了什么?它适用于硬编码阵列或刀片打印的Json阵列。
    我甚至尝试过:

    $(document).ready(function() { $("#tokenfield").tokenInput("localhost/inkrasa3/public/hashes", {theme:"facebook",tokenLimit:5,preventDuplicates:true,tokenValue:"name",method:"post"}); });

    路线:

    `Route::post('hashes',function(){
    $names[] = array('id' => 0, 'name' => 'hello');
    $names[] = array('id' => 1, 'name' => 'sup');
    
    return Response::json($names);
    

    });` 两次我都 404 查看浏览器开发工具时出错。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Community CDub    4 年前

    您的响应应为“application/json”类型。

    尝试以下代码 拉阿维尔4 :

    Route::get('hashes', function()
    {
        $names[] = array('id' => 0, 'name' => 'hello');
        $names[] = array('id' => 1, 'name' => 'sup');
    
        return Response::json($names);
    });
    

    对于 拉阿维尔5 将return语句替换为:

    return response()->json($names);

    动态响应示例(Laravel 4)

    Route::get('hashes', function()
    {
        // submitted letters from TokenInput
        $letters = Input::get('q');
    
        // search in the column "name"
        $users = User::where('name', 'LIKE', '%' . $letters . '%')->get();
    
        return Response::json($users->toArray());
    });
    

    当然是用户表:)

    +----+--------------+
    | id | name         |
    +----+--------------+
    |  1 | Peter        |
    |  2 | Andy         |
    |  3 | Walter       |
    |  4 | ...          |
    +----+--------------+