当 PUT 通过 AJAX 用于 Laravel 资源时,方法不允许 [英] 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
opener 方法)进行标准表单提交时,这很好用.
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.
这篇关于当 PUT 通过 AJAX 用于 Laravel 资源时,方法不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!