Laravel Send Data Such As 1-2-3 By Ajax And Get Missing Argument Error
i try to use below ajax to send data such as 1-2-3-4-5
to controller action but i get error:
Ajax:
$.ajax({
type: "GET",
url: " {{ url('changeMenuItemOrders') }}",
data: {orders: "1-2-3"},
success: function (data) {
}
});
Route:
Route::get('changeMenuItemOrders','[email protected]');
changeMenuItemOrders
action:
public function changeMenuItemOrders($orders)
{
dd($orders);
}
Firebug:
http://localhost/sample/public/changeMenuItemOrders?orders=1-2-3 500 Internal Server Error
Laravel error:
ErrorException in SystemController.php line 114: Missing argument 1 for App\Http\Controllers\SystemController::changeMenuItemOrders()
Answer
The issue you're facing is query string parameters vs routing parameters. Right now, you have:
public function changeMenuItemOrders($orders)
looking for the route parameter $orders
. In order to make this work, you would require the route:
Route::get('changeMenuItemOrders/{orders}','[email protected]');
and you would access this function by navigating to (GET
):
http://localhost/sample/public/changeMenuItemOrders/1-2-3
Since you are creating a query string via your ajax request, you shouldn't have $orders
in your function, but you should instead be accessing the orders via the GET
array, using
$orders = Input::get('orders');
Hope that helps clear things up. Also, as a side note, if you're passing multiple orders (ie 1, 2 and 3), consider posting an orders[]
(orders[0] 1, orders[1] 2, orders[2] 3
) instead of a string ("1-2-3"
) that you would have to split on the backend.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?