如何在 Laravel 中使布局错误 如果请求是 ajax



我试图使用简单的获取请求加载页面

<script>
$(document).ready(function(){
$("li").click(function(e){
e.preventDefault();
var href = $("a",this).attr('href');
$.ajax({
async: true,
type: "GET",
url: href,
success: function (response) {
$('#main-content').html(response);
}
})
});
});
</script>

作为回应,我正在获得完整布局的内容。但在这里,我试图只获取没有布局的内容。在控制器中,我编写了如下代码

public function index()
{
if ($request->ajax()) 
{
$this->layout = null; //but same result 
}
$tags = Tag::orderBy('id', 'desc')->paginate(10);
return view('admin.tags.index')->with('tags',$tags);
}

但是我的布局得到了相同的结果,我怎样才能使布局错误?或者可以更改控制器中的布局?

您可以使用 renderSections

return view('admin.tags.index')->renderSections()['content'];

https://laravel-tricks.com/tricks/render-view-without-layout

好吧,您仍在返回view.为什么不做:return response()->json(["data"=>"some data"]);在阿贾克斯检查之后?这会将 JSON 响应返回到前端。

相关内容

最新更新