问题描述
我正在尝试使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!