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

Error 405 (Method Not Allowed) Laravel 5(错误 405(方法不允许)Laravel 5)
本文介绍了错误 405(方法不允许)Laravel 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的代码:

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

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

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

控制器

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

Jquery 错误是 http://localhost/laravel5.1/public/empresas/eliminar/5 405(方法不允许).

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

网址值为

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

而数据值为

_method=DELETE&_token=pCETpf1jDT1rY615o62W0UK7hs3UnTNm1t0vmIRZ.

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

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

有人可以帮我吗?

谢谢.

编辑!!

路线

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

推荐答案

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

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

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

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

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

这篇关于错误 405(方法不允许)Laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)