le*_*leo 2 php ajax json laravel
这是我的控制器:
public function index(Request $request)
{
$items = Post::latest()->paginate(5);
$cmnt = Comment::all();
return response()->json(array('posts'=>$items,'comment'=>$cmnt));
}
Run Code Online (Sandbox Code Playgroud)
这是我的 ajax 请求
function getPageData() {
$.ajax({
dataType: 'json',
url: "{{route('post_status.index')}}",
data: {page:1}
}).done(function(data){
manageRow(data.data);
});
}
function manageRow(data) {
console.log(data.comment);
}
Run Code Online (Sandbox Code Playgroud)
为什么我收到错误?帮我摆脱这个plzz
如果不返回视图,laravel 默认返回 json,在你的情况下 index() 应该返回:
return ['posts' => $items,'comment' => $cmnt];
Run Code Online (Sandbox Code Playgroud)
我也不认为这是正确的
{{route('post_status.index')}}
Run Code Online (Sandbox Code Playgroud)
大概应该是
{{ url('post_status/index') }}
Run Code Online (Sandbox Code Playgroud)