如何在 Laravel 中使布局为假 如果请求是 ajax

How to make layout false in Laravel If request is 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);
}

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

好吧,你还在 returnview。为什么不这样做: return response()->json(["data"=>"some data"]); 在你 Ajax 检查之后?这将 return 对您的前端做出 JSON 响应。

您可以使用 renderSections

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

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