Laravel AJAX 表单帖子显示500个内部服务器错误,我尝试了很多解决方案,但仍然不起作用

5us2dqdw  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(103)

我想用 AJAX 请求发布数据,但它说内部服务器。我尝试添加 meta数据和X-CSRF-TOKEN,但仍然不工作。请看我的代码
AJAX 代码:

$("#firstForm").on("submit", (e)=>{
    e.preventDefault()
    let dataString = $(this).serialize();
    let email = document.getElementById("emailInput").value
    let password = document.getElementById("passwordInput").value
    var token = $('meta[name="csrf-token"]').attr('content');
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': token
        }
    });
    $.ajax({
        type: 'POST',
        url: '/register/create',
        data: dataString,
        dataType: 'json',
    }).done(function(response){
        console.log("Done");
    });
    return false;
    })

HTML表单:

<form class="mt-5 text-start" id="firstForm" method="post">
                    <label class="text-white main-font">Email</label>
                    <input type="email" name="email" id="emailInput" class="form-control mb-2" placeholder="Enter your email here">
                    <label class="text-white main-font">Password</label>
                    <input type="password" name="password" id="passwordInput" class="form-control password mb-2" placeholder="Enter your password here">
                    <i class="d-none fa-solid fa-eye fs-5 eye" onclick="eyeOpen()"></i>
                    <i class="fa-solid fa-eye-slash fs-5 eye" onclick="eyeClose()"></i>
                    <div class="form-check text-start mb-5">
                        <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
                        <label class="form-check-label text-white" for="flexCheckDefault">
                            I've agree to the terms and conditions!
                        </label>
                    </div>
                    <button id="firstBtn" class="mb-3 mt-5 btn btn-lg btn-danger text-white main-font w-100">Next</button>
                </form>

拉勒维尔路线:

Route::post('register/create', [AccountController::class, 'create']);

Laravel控制器:

public function create(Request $request) {
    $user = new User;
    $user->email = $request->email;
    $user->password = Hash::make($request->password);
    $user->save();

    return view('accounts.login');
}

错误:

[2022-11-22 13:18:23] local.ERROR: SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `users` (`email`, `password`, `updated_at`, `created_at`) values (?, $2y$10$uwsmx9lDw4z9a0tGwUjBWeNM8zfNEkoa7oREGdCBgxTkF3Owlo5Uy, 2022-11-22 13:18:23, 2022-11-22 13:18:23)) {"exception":"[object] (Illuminate\\Database\\QueryException(code: HY000): SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into `users` (`email`, `password`, `updated_at`, `created_at`) values (?, $2y$10$uwsmx9lDw4z9a0tGwUjBWeNM8zfNEkoa7oREGdCBgxTkF3Owlo5Uy, 2022-11-22 13:18:23, 2022-11-22 13:18:23)) at C:\\xampp\\htdocs\\dating\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:712)
zz2j4svz

zz2j4svz1#

创建新用户时,“名称”字段是必需的,并且错误还指出名称没有值。
试着在你的表单中添加一个名字并通过 AJAX 发送它。就像这样

<form class="mt-5 text-start" id="firstForm" method="post">
@csrf
                    <label class="text-white main-font">Name</label>
                    <input type="text" name="name" id="name" class="form-control mb-2" placeholder="Enter your Name here">

                    <label class="text-white main-font">Email</label>
                    <input type="email" name="email" id="emailInput" class="form-control mb-2" placeholder="Enter your email here">
                    <label class="text-white main-font">Password</label>
                    <input type="password" name="password" id="passwordInput" class="form-control password mb-2" placeholder="Enter your password here">
                    <i class="d-none fa-solid fa-eye fs-5 eye" onclick="eyeOpen()"></i>
                    <i class="fa-solid fa-eye-slash fs-5 eye" onclick="eyeClose()"></i>
                    <div class="form-check text-start mb-5">
                        <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
                        <label class="form-check-label text-white" for="flexCheckDefault">
                            I've agree to the terms and conditions!
                        </label>
                    </div>
                    <button id="firstBtn" class="mb-3 mt-5 btn btn-lg btn-danger text-white main-font w-100">Next</button>
                </form>

并且像这样改变控制器

public function create(Request $request) {
    $user = new User;
    $user->name= $request->name;
    $user->email = $request->email;
    $user->password = Hash::make($request->password);
    $user->save();

    return view('accounts.login');
}

像这样修改 AJAX

let dataString = $(this).serialize();
                $.ajax({
                    type: "post",
                    url: '/register/create',
                    data: dataString, // serializes the form's elements.
                    success: function(data) {
                        console.log(data);
                    },
                    error: function(data) {
                        console.log(data['message']);
                    },
                });
gopyfrb3

gopyfrb32#

我检查了laravel.log文件的错误信息。发现Jquery序列化返回空字符串。因此,我自定义编写了serialize函数,以获取最接近的dataString作为Jquery。

let dataString = "email=" + document.getElementById("emailInput").value + "&password=" + document.getElementById("passwordInput").value

相关问题