javascript - Send array to controller by Ajax call, then controller return another View with that array in Laravel

I am passing array to controller by ajax. Controller is accessing array and return response successfully. But I want that when controller get array it return different view(suppose A.blade.php) and I can use that array in A.blade.php.

I have seen many replies like use window.location="url" in success:function(){} but it will only go to view without array.

Only purpose is to pass array to controller and controller return another view with array and i don't need of response.

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);    
    
    }

Routes.web.php

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

Answer

Solution:

Based on your comments, you could dynamically create a form and add the array you want to a hidden element. Then submit the form.

Untested code:

$(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();
                        }
                    );
    }
);

Answer

Solution:

To send an array from view to controller and from controller to other view: First create a form and use onsubmit attribute

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

Then write function for onsubmit


<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>

Routes

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

In Controller

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

In blade.view, access array as

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

It worked for me.

Source