I've got this resource in routes.php:
Route::resource('items', 'ItemsController', ['before' => 'admin_access']);
Trying to reach ItemsContoller@update
method through AJAX but it's kicking out a 405 Method not allowed
error:
var $inputs = $('input', row);
var id = $(row).find('.edit').data('id');
var data = $inputs.serializeJSON();
data['_token'] = $('input[name=_token]').val();
data['_method'] = 'PUT';
console.debug(data);
$.ajax({
url: 'items/' + id,
method: 'PUT',
dataType: 'json',
data: data,
complete: function (data) {
if (data.success) {
itemsTable.ajax.reload();
}
}
});
Both the id
and data
variables contain the correct information.
This works fine when I do a standard form submission with PUT as the method (using anahkiasen/Former
opener method).
What am I missing here?
The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.
In Laravel, we can use $request->ajax() method to check request is ajax or not.
To use AJAX in Laravel, you need to import a jquery library in your view file to use ajax functions of jquery, which will be used to send and receive data using ajax from the server.
Most browsers can't send PUT methods and are restricted to just GET and POST.
Try changing the method to POST, but leave your _method
element in the data
array to spoof the request method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With