Laravel 8更新提交数据时使用ajax报错

问题描述 投票:0回答:0

我正在尝试通过打开模态引导程序来更新数据,并且在将数据从 AJAX 放入路由时出现错误
这是错误 'localhost:8000/nilaiUpdates/1 419(未知状态)

我厌倦了改变这么多方法,但它对我不起作用。

所以这是我的路线:

Route::put('/nilaiUpdates/{id}', [PesertaController::class, 'update']);

My Blade 文件 nilaiPeserta.blade.php :

<div id="editPesertaModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                 <form id="update-user-form"> 
                    <div class="modal-header">
                        <h4 class="modal-title">Edit Peserta</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>Name</label>
                            <input  id="id" name="id" type="text" class="form-control" readonly >
                            <input  id="nama" name="nama"  type="text" class="form-control" required >
                        </div>
                        <div class="form-group">
                            <label>Email</label>
                            <input  id="email" name="email" type="email" class="form-control" required >
                        </div>
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                        <input type="submit" class="btn btn-info update-btn" value="Save">
                    </div>
                </form>
            </div>
        </div>
    </div>

我的 AJAX:
错误来自这里

        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('.update-btn').on('click', function (e) {
            e.preventDefault();

            let id = $('#id').val();

            var data = {
                'nama' : $('#nama').val(),
                'email' : $('#email').val(),
            }

            $.ajax({
                type: "PUT",
                url: "/nilaiUpdates/" + id,
                data: data,
                dataType : 'json',
                success: function (response) {
                    console.log(response);
                },
                error: function (xhr) {
                    console.log(xhr.responseText);
                }
            });
        });

我的控制器:
PesertaController.php

public function update(Request $request, $id)
{
    $this->validate($request,[
       'nama' => 'required',
       'email' => 'required',
    ]);

    $peserta = Peserta::find($id);

    $peserta->nama = $request->nama;
    $peserta->email = $request->email;
    $peserta->update();

    return response()->json([
        'status'=>200,
        'message' => 'Peserta Updated Successfuly',
    ]);
}

我的模特:

class Peserta extends Model
{
    use HasFactory;

    protected $table = "peserta";
    protected $fillable =
    [
        'nama'
        ,'email'
    ];
}

请帮助我:)

ajax laravel-8
© www.soinside.com 2019 - 2024. All rights reserved.