繁体   English   中英

通过 Ajax 调用将数组发送到 controller,然后 controller 在 Laravel 中返回另一个包含该数组的视图

[英]Send array to controller by Ajax call, then controller return another View with that array in Laravel

我通过 ajax 将数组传递给 controller。Controller 正在访问数组并成功返回响应。 但我希望当 controller 获取数组时它返回不同的视图(假设 A.blade.php)并且我可以在 A.blade.php 中使用该数组。

我看到很多回复,比如在success:function(){}中使用window.location="url"但如果没有数组,它只会显示 go。

唯一的目的是将数组传递给 controller 和 controller 返回另一个带有数组的视图,我不需要响应。

AJAX function


    $(function(){
       $('#but').click(function() {
         alert("Button Click");
        ;
         $.ajax({
           type: 'get',
           url: '/suck',
           data: {arr},
   success: function( data ) {
    
    document.getElementById("p").innerHTML =data;
    
      
      },
   error: function(xhr, status, error) {
    alert(error);
    
   },
  dataType: 'text'
});
       });
    }); 

Controller

 public function getAjax(Request $req)
    {
        $input=$req->all();     
// Here I want when controller access array it return another view with array and no need of response
        return response()->json($input);    
    
    }

路线.web.php

Route::get('/suck',[ajaxcontroller::class,'getAjax']);

根据您的评论,您可以动态创建一个表单并将您想要的数组添加到隐藏元素中。 然后提交表格。

未经测试的代码:

$(function()
    {
    $('#but').click ( 
                    function()
                        {   
                        var myArray = [];
                        var myJson = JSON.stringify(myArray); 

                        var myForm = $(document.createElement('form'));
                        $(myForm).attr("action", "/url/to/controller");
                        $(myForm).attr("method", "POST");
                        
                        var input = $("<input>").attr("type", "hidden").attr("name", "myArray").val(myJson);                       
                        
                        $(form).append($(input));
                    
                        $(form).submit();
                        }
                    );
    }
);

要将数组从视图发送到 controller 并将数组从 controller 发送到其他视图:首先创建一个表单并使用 onsubmit 属性

<form id="myid" action="/go" method="post" onsubmit="submitForm(event)">
@csrf
         <input type="submit" value="submit">
</form>

然后onsubmit写function


<script  type="text/JavaScript">
function submitForm(event){  
  var arr=["Tile","Desk","Door"];  // Array which has to pass to controller
    var i;
       for(i=0;i<arr.length;i++){ // create and append arr.length times
          var input = $("<input>").attr("type", "hidden").attr("name", "myArray["+i+"]").val(arr[i]);                       
             $(myid).append($(input));   // append to form              
    }   
      this.submit();  
  }
</script>

航线

Route::post('/go',[subcontroller::class,'getArray']);

在 Controller

class subcontroller extends Controller
{
    public function getArray(Request $req)
    {     
    $arr=$req->get('myArray');   
     return view('viewName',['set'=>$arr]);
    }
 }

在 blade.view 中,访问数组为

@foreach($set as $item)
<div>{{$item}}</div>
@endforeach

它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM