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']);
My Answer:
'/url/to/controller' but it is not working for me. Can anyone help me with this?
Here is an example of how you can achieve this in Laravel:
1. Create a route in your web.php file:
php
Route::post('/process-array', 'YourController@processArray');
2. Create a controller method in YourController.php:
php
public function processArray(Request $request) {
$array = $request->input('array');
// Process the array here
return view('A', ['array' => $array]);
}
3. Make an AJAX call in your JavaScript file:
javascript
var array = [1, 2, 3, 4, 5];
$.ajax({
type: 'POST',
url: '/process-array',
data: { array: array },
success: function(response) {
// Redirect to the new view
window.location.href = '/url/to/A.blade.php';
}
});
4. Create a blade view file A.blade.php in your resources/views directory:
html
A View
Array from controller:
@foreach($array as $item)
- {{ $item }}
@endforeach
Now, when you make the AJAX call to the controller, it will process the array and return the A.blade.php view with the array data displayed on the page.
Rate this post
5 of 5 based on 2759 votesComments