这是我在页面中使用的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错误。
您的响应类型应该是"application/json";。
尝试Laravel 4的以下代码:
Route::get('hashes', function()
{
$names[] = array('id' => 0, 'name' => 'hello');
$names[] = array('id' => 1, 'name' => 'sup');
return Response::json($names);
});
对于Laravel 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 | ... |
+----+--------------+