php 错误 405(方法不允许)Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31622818/
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-08-25 22:32:59  来源:igfitidea点击:

Error 405 (Method Not Allowed) Laravel 5

phpjquerylaravellaravel-5

提问by German Ortiz

Im trying to do a POST request with jQuery but im getting a error 405 (Method Not Allowed), Im working with Laravel 5

我正在尝试使用 jQuery 执行 POST 请求,但我收到错误 405(不允许方法),我正在使用 Laravel 5

THis is my code:

这是我的代码:

jQuery

jQuery

<script type="text/javascript">
    $(document).ready(function () {
        $('.delete').click(function (e){
            e.preventDefault();
            var row = $(this).parents('tr');
            var id = row.data('id');
            var form = $('#formDelete');
            var url = form.attr('action').replace(':USER_ID', id);
            var data = form.serialize();
            $.post(url, data, function (result){
                alert(result);
            });
        });
    });
    </script>

HTML

HTML

{!! Form::open(['route' => ['companiesDelete', ':USER_ID'], 'method' =>'DELETE', 'id' => 'formDelete']) !!}

    {!!Form::close() !!}

Controller

控制器

public function delete($id, \Request $request){
        return $id;
    }

The Jquery error is http://localhost/laravel5.1/public/empresas/eliminar/5405 (Method Not Allowed).

Jquery 错误是http://localhost/laravel5.1/public/empresas/eliminar/5405 (Method Not Allowed)。

The url value is

网址值为

http://localhost/laravel5.1/public/empresas/eliminar/5

and the data value is

并且数据值是

_method=DELETE&_token=pCETpf1jDT1rY615o62W0UK7hs3UnTNm1t0vmIRZ.

If i change to $.getrequest it works fine, but i want to do a post request.

如果我改为$.get请求它工作正常,但我想做一个发布请求。

Anyone could help me?

任何人都可以帮助我吗?

Thanks.

谢谢。

EDIT!!

编辑!!

Route

路线

Route::post('empresas/eliminar/{id}', ['as' => 'companiesDelete', 'uses' => 'CompaniesController@delete']);

回答by Jeemusu

The methodNotAllowedexception indicates that a route doesn't exist for the HTTP method you are requesting.

methodNotAllowed异常表明您请求的 HTTP 方法不存在路由。

Your form is set up to make a DELETErequest, so your route needs to use Route::delete()to receive this.

您的表单已设置为发出DELETE请求,因此需要使用您的路由Route::delete()来接收此请求。

Route::delete('empresas/eliminar/{id}', [
        'as' => 'companiesDelete',
        'uses' => 'CompaniesController@delete'
]);

回答by davsp

Your routes.php file needs to be setup correctly.

您的 routes.php 文件需要正确设置。

What I am assuming your current setup is like:

我假设您当前的设置是这样的:

Route::post('/empresas/eliminar/{id}','CompanyController@companiesDelete');

or something. Define a route for the delete method instead.

或者其他的东西。而是为 delete 方法定义一个路由。

Route::delete('/empresas/eliminar/{id}','CompanyController@companiesDelete');

Now if you are using a Route resource, the default route name to be used for the 'DELETE' method is .destroy. Define your delete logic in that function instead.

现在,如果您使用的是 Route 资源,则用于“DELETE”方法的默认路由名称是 .destroy。而是在该函数中定义删除逻辑。

回答by Paolo

This might help someone so I'll put my inputs here as well.

这可能会对某人有所帮助,所以我也会在这里提出我的意见。

I've encountered the same (or similar) problem. Apparently, the problem was the POST request was blocked by Modsec by the following rules: 350147, 340147, 340148, 350148

我遇到了相同(或类似)的问题。显然,问题是 POST 请求被 Modsec 通过以下规则阻止:350147、340147、340148、350148

After blocking the request, I was redirected to the same endpoint but as a GET request of course and thus the 405.

阻止请求后,我被重定向到同一个端点,但当然是作为 GET 请求,因此是 405。

I whitelisted those rules and voila, the 405 error was gone.

我将这些规则列入白名单,瞧,405 错误消失了。

Hope this helps someone.

希望这可以帮助某人。

回答by Adam Brzeziński

If you're using the resource routes, then in the HTML body of the form, you can use method_field helperlike this:

如果您使用的是资源 routes,那么在表单的 HTML 正文中,您可以像这样使用method_field 助手

<form>
  {{ csrf_field() }}
  {{ method_field('PUT') }}
  <!-- ... -->
</form>

It will create hidden form input with method type, that is correctly interpereted by Laravel 5.5+.

它将创建具有方法类型的隐藏表单输入,由 Laravel 5.5+ 正确解释。

Since Laravel 5.6 you can use following Blade directives in the templates:

从 Laravel 5.6 开始,您可以在模板中使用以下 Blade 指令:

<form>
  @method('put')
  @csrf
  <!-- ... -->
</form>

Hope this might help someone in the future.

希望这可能会在将来对某人有所帮助。

回答by Mahbubul Alam

When use method delete in form then must have to set route delete

在表单中使用方法删除时,必须设置路由删除

Route::delete("empresas/eliminar/{id}", "CompaniesController@delete");

回答by Cornel Raiu

In my case the route in my router was:

就我而言,路由器中的路由是:

Route::post('/new-order', 'Api\OrderController@initiateOrder')->name('newOrder');

Route::post('/new-order', 'Api\OrderController@initiateOrder')->name('newOrder');

and from the client app I was posting the request to:

从客户端应用程序我将请求发布到:

https://my-domain/api/new-order/

https://my-domain/api/new-order/

So, because of the trailing slash I got a 405. Hope it helps someone

所以,由于尾部斜杠,我得到了 405。希望它可以帮助某人