在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:02:47  来源:igfitidea点击:

Method not allowed when PUT used over AJAX for Laravel resource

jqueryajaxlaravellaravel-4laravel-routing

提问by eComEvo

I've got this resource in routes.php:

我在 routes.php 中有这个资源:

Route::resource('items', 'ItemsController', ['before' => 'admin_access']);

Trying to reach ItemsContoller@updatemethod through AJAX but it's kicking out a 405 Method not allowederror:

试图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 idand datavariables contain the correct information.

无论是iddata变量包含正确的信息。

This works fine when I do a standard form submission with PUT as the method (using anahkiasen/Formeropener method).

当我使用 PUT 作为方法(使用anahkiasen/Formeropener 方法)进行标准表单提交时,这很好用。

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 _methodelement in the dataarray to spoof the request method.

尝试将方法更改为 POST,但将您的_method元素留在data数组中以欺骗请求方法。