【发布时间】:2022-01-13 14:48:39
【问题描述】:
我使用 jquery ajax 更新数据库并根据返回值打印“成功/失败”并发出警报。产品更新成功,但屏幕上显示“失败”消息,并收到错误“此路由不支持 PUT 方法”。
我的 jquery:
$('#policies_button').on('click', function () {
$('#ShiftAddModal').modal('hide');
var id = $('#select_policies').val();
var url = $('#selected_option_' + id).attr('data-url');
$.ajax({
type: 'PUT',
data: $('#bulkupdate_form').serialize(),
url: url,
success: function (response) {
if (response.success) {
$('#ShiftAddModal').modal('hide');
alertify.success('Başarılı: İzin kuralı ataması başarılı bir şekilde gerçekleşmiştir.');
} else if (response.error) {
alertify.error(response.error);
}
},
error: function (e) {
alertify.error('Hata: Sayfanızı yenileyerek tekrar deneyiniz.');
}
});
});
网址:
<option id="selected_option_{{$item->policies_id}}"
value="{{$item->policies_id}}"
data-url="{{route('personnel.update',$item->policies_id)}}">{{$item->name}}</option>
通过路线来这里:
protected function policiesAction(Request $request, $id)
{
foreach ($this->model->get() as $item) {
$action = Actions::personnel_policies_update(
$this->model,
$request->get('personnel_name' . $item->personnel_id),
$id
);
}
return $action;
}
public static function personnel_policies_update(Model $model,$personnel_id,$id){
$fields = array(
'policies_id' => $id,
);
$model->where('personnel_id',$personnel_id)->update($fields);
return redirect()->to(route('bulkupdate.index'))->with('success','Başarılı: Personel ataması başarıyla gerçekleşti!');
}
【问题讨论】:
-
似乎您没有为请求的
url定义 PUT 路由。尝试将方法更改为 POST on ajax 请求,(即type: 'POST') -
Route::resource('personnel' => \App\Http\Controllers\backend\PersonnelController::class);
-
好,把方法改成POST,注入一个csrf_token,或者在headers中添加。