当通过AJAX在Laravel资源上使用PUT时,不允许使用该方法 [英] Method not allowed when PUT used over AJAX for Laravel resource
问题描述
我在routes.php中有此资源:
I've got this resource in routes.php:
Route::resource('items', 'ItemsController', ['before' => 'admin_access']);
试图通过AJAX到达ItemsContoller@update
方法,但它踢出了405 Method not allowed
错误:
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();
}
}
});
id
和data
变量都包含正确的信息.
Both the id
and data
variables contain the correct information.
当我使用PUT作为方法(使用anahkiasen/Former
开瓶器方法)进行标准表单提交时,此方法很好用.
This works fine when I do a standard form submission with PUT as the method (using anahkiasen/Former
opener method).
我在这里想念什么?
推荐答案
大多数浏览器无法发送PUT方法,并且仅限于GET和POST.
Most browsers can't send PUT methods and are restricted to just GET and POST.
尝试将方法更改为POST,但将_method
元素保留在data
数组中以欺骗请求方法.
Try changing the method to POST, but leave your _method
element in the data
array to spoof the request method.
这篇关于当通过AJAX在Laravel资源上使用PUT时,不允许使用该方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!