【问题标题】:The "PUT method is not supported for this route" error comes up after jquery ajax updatejquery ajax更新后出现“此路由不支持PUT方法”错误
【发布时间】: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中添加。

标签: php jquery ajax laravel


【解决方案1】:

将方法更改为 POST。

$.ajax({
   type: 'POST',
   data: $('#bulkupdate_form').serialize(),
   url: url,
   ...
});

考虑到 csrf 令牌尚未包含在标题中,您必须将其包含在表单数据中:

<form id="bulkupdate_form" ... >
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <!-- or simply -->
    @csrf        
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-14
    • 2021-03-12
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2022-08-17
    相关资源
    最近更新 更多