在 Laravel 资源上通过 AJAX 使用 PUT 时不允许方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31631206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method not allowed when PUT used over AJAX for Laravel resource
提问by eComEvo
I've got this resource in routes.php:
我在 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:
试图ItemsContoller@update
通过 AJAX访问方法,但它抛出了一个405 Method not allowed
错误:
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.
无论是id
和data
变量包含正确的信息。
This works fine when I do a standard form submission with PUT as the method (using anahkiasen/Former
opener method).
当我使用 PUT 作为方法(使用anahkiasen/Former
opener 方法)进行标准表单提交时,这很好用。
What am I missing here?
我在这里错过了什么?
回答by Martin Bean
Most browsers can't send PUT methods and are restricted to just GET and POST.
大多数浏览器不能发送 PUT 方法,并且仅限于 GET 和 POST。
Try changing the method to POST, but leave your _method
element in the data
array to spoof the request method.
尝试将方法更改为 POST,但将您的_method
元素留在data
数组中以欺骗请求方法。