我想将此 JSON 数据传递给某个 View ,但不知道它是如何工作的。
我也使用过,也制作过 View ,并将此数据转换为 JSON 并通过其他方式传递,但没有用
$items = Items::all();
return response()->JSON($items);
例如 View 是
items.create
最佳答案
对于 Laravel 4.2 版,
您可以将您的数据传递给您的 Blade 使用具有将在 Blade 中使用的数组的变量(例如:$data)查看。
$flag 是在代码的 JS 部分中使用的变量,因此您也可以将其作为数组传递:Response::json(['param1' => $foo1, 'param2' =>$foo2)]);
在您的 Controller 中返回 View :
return Response::json(['view' => View::make('yourbladename', $data)->render(), 'flag'=>$flag]);
在您的 JS 中,将数据变量用作:
function(data){
$('#DivToAppendHTML').append(data.view); //this appends html blade to the Div having the ID DivToAppendHTML
if(data.flag == 1){ //this uses the second variable passed in controller for any other purpose
$('.classname').remove();
}
}
关于json - 如何传递JSON数据以在laravel中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39308057/