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']);
2
Answers
To send an array from view to controller and from controller to other view: First create a form and use onsubmit attribute
Then write function for onsubmit
Routes
In Controller
In blade.view, access array as
It worked for me.
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: