简体   繁体   English

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

[英]jQuery TokenInput not working (displaying search results ) with laravel

This is the javascript code I use in my page. 这是我在页面中使用的javascript代码。

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

And here is the route file. 这是路由文件。

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

What am I doing wrong? 我究竟做错了什么? It works perfectly for hard coded array or an Json array printed by blade. 它非常适用于硬编码阵列或刀片打印的Json阵列。
I have even tried this: 我什至尝试过:

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

with the route: 与路线:

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

return Response::json($names);

});` At both times I get 404 error when I looked in browser dev tools. });`在浏览器开发工具中,这两次我都收到404错误。

Your response should be of type "application/json". 您的响应应为“ application / json”类型。

Try the following code for Laravel 4 : Laravel 4尝试以下代码:

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

    return Response::json($names);
});

And for Laravel 5 replace the return statement with: 对于Laravel 5,将return语句替换为:

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

example with dynamic response (Laravel 4) 动态响应的示例(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());
});

the user table of course :) 用户表,当然:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM