Laravel @include 通过 ajax 显示刀片视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46448937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:43:58  来源:igfitidea点击:

Laravel @include a blade view via ajax

javascriptjqueryajaxlaravelblade

提问by Cowgirl

I have a page which will @include some content, I want to @include that blade view file using ajax request. How should I do it.

我有一个页面将@include 一些内容,我想使用ajax 请求@include 该刀片视图文件。我该怎么做。

Basically, the view file will get the items from server,

基本上,视图文件将从服务器获取项目,

**price.blade.php**
@foreach ($items as $item)
<div class="item-post">
  <div class="priceofitem">{{ $item->price }} </div>

I want to include @include('price.blade.php') in my tab section

我想在我的选项卡部分包含 @include('price.blade.php')

<ul class="tabs">
<li><a href="#tab1">Prices </li>
<div id="tab1">@include('price.blade.php')</div>

I don't want to include that view file automatically upon load, as I don't want to load that tab's content unless the user clicks on it, if the user wants the prices than the user clicks on that tab, and an AJAX request will be sent to include that file.

我不想在加载时自动包含该视图文件,因为我不想加载该选项卡的内容,除非用户点击它,如果用户想要价格而不是用户点击该选项卡,以及一个 AJAX 请求将被发送以包含该文件。

Hope I made myself clear, please let me know if you did not understand me.

希望我说清楚了,如果您不理解我,请告诉我。

Fingers crossed

十指交叉

回答by Dhiraj

You want something like

你想要类似的东西

$(document).ready(function() {
    $("#tab1").click(function() {
        $.ajax({
            type: 'POST', 
            url : "/yourrouteview", 
            success : function (data) {
                $("#tab1").html(data);
            }
        });
    });
}); 

Your controller and route must configure /yourrouteview to get the correct view (that is @include('price.blade.php')

您的控制器和路由必须配置 /yourrouteview 以获得正确的视图(即 @include('price.blade.php')

回答by Mayank Pandeyz

Make your ajax request and return the view from controller function like:

发出 ajax 请求并从控制器函数返回视图,例如:

return view('your_view');

in ajax successfunction, append it to where you want like:

在 ajaxsuccess函数中,将其附加到您想要的位置,例如:

success: function(response){
    $('#Id').html(response);
}

The flow is something like:

流程类似于:

$('#tab').click(function(){
    // ajax call here
    ...
    success: function(response){
        $('#Id').html(response);
    }
});

controller:

控制器:

function funcName()
{
    // Do what ever you want
    return view('your_view');
}

回答by Harsh Joshi

Make ajax call with id with using blade engine with get request in laravel like this!! Try this.

像这样在 laravel 中使用带有 get 请求的刀片引擎使用 id 进行 ajax 调用!!尝试这个。

$(document).ready(function(){
  var id = $(this).data("id");
  $.ajax({
     type: "GET",
     url:"{{ url('your url') }}/"+id,
     cache: false,
     contentType: false,
     processData: false,
     success: function (data) {
       $("#id").html(data);
     },
     error: function (xhr, textStatus, errorThrown) {
       console.log("XHR",xhr);
       console.log("status",textStatus);
       console.log("Error in",errorThrown);
     }
  });

回答by Jukka Newkampton

Via ajax request you can get compiled view from server and yield that value into some parent root, e.g in promise .then call.

通过 ajax 请求,您可以从服务器获取编译视图并将该值生成到某个父根目录中,例如在 promise .then 调用中。

On the server side you can use simple handler of HTTP request in your routes:

在服务器端,您可以在路由中使用 HTTP 请求的简单处理程序:

Route::get('/', function () {
    return view('price'); // here you can pass params for compiling blade template
});