我试图用jQuery做一个POST请求,但是我得到错误405(方法不允许),我使用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(方法不允许).
url值是
http://localhost/laravel5.1/public/empresas/eliminar/5
数据值为
_method=DELETE&_token=pCETpf1jDT1rY615o62W0UK7hs3UnTNm1t0vmIRZ.
如果我更改为$.get请求,它可以正常工作,但我想做一个post请求.
任何人都可以帮助我
谢谢.
编辑!!
路线
Route::post('empresas/eliminar/{id}',['as' => 'companiesDelete','uses' => 'CompaniesController@delete']);
methodNotAllowed exception表示您要请求的HTTP方法不存在路由.
原文链接:https://www.f2er.com/laravel/131888.html您的表单设置为进行DELETE请求,因此您的路由需要使用Route :: delete()来接收此请求.
Route::delete('empresas/eliminar/{id}',[ 'as' => 'companiesDelete','uses' => 'CompaniesController@delete' ]);