Ad

Laravel Send Data Such As 1-2-3 By Ajax And Get Missing Argument Error

- 1 answer

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()
Ad

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.

Ad
source: stackoverflow.com
Ad