【问题标题】:Laravel Ajax form post shows 500 internal server error. I tried solving with a lot of solution but still not workingLaravel Ajax 表单发布显示 500 内部服务器错误。我尝试了很多解决方案但仍然无法解决
【发布时间】:2022-11-22 22:50:13
【问题描述】:

我想用 ajax 请求发布数据,但它说是内部服务器。我尝试添加元数据和 X-CSRF-TOKEN 但仍然无法正常工作。请看看我的代码

阿贾克斯代码:

$("#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']);

控制器:

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)

【问题讨论】:

  • 500 错误是一种通用错误消息,几乎涵盖了 PHP 脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。对于 Laravel,还要检查storage/logs/ 中的日志
  • 我找到了日志,但我应该如何处理它?
  • 他们?找资料什么的引起的错误 ...?
  • 有超过 2k 行。我应该如何阅读以及我应该找到什么?
  • 错误通常接近尾声。如果有堆栈跟踪(在行的开头有数字),则查看 #0 所在的位置。如果是Laravel日志,看最后一行有时间戳

标签: javascript php ajax laravel


【解决方案1】:

创建新用户时,名称字段是必需的,错误还表明该名称没有值。

尝试在您的表单中添加一个填写的名称并通过 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']);
                    },
                });

【讨论】:

  • 我试过了。现在即使我正在输入数据,表单也没有提供任何数据。但是可以使用 getElementById().value 注销数据。我使用 jquery 序列化表单数据并将其作为数据发送。我认为这是目前的主要问题。我该如何解决?我应该摆脱序列化吗?
  • 我已经更新了我的答案检查表单和 ajax
  • 当我使用你的答案时它说 405 方法不允许。我看到 ajax 有 delete 方法。我的原始表单是发布数据。所以,我将其更改为发布。然后,它说 419 未知状态错误,这可能是因为缺少 X-CSRF-TOKEN。所以,我也补充说。然后,它说 500 internal server error 并且你发出的警报说 undefined 这是当前的问题
  • 我以@csrf 的形式添加了 X-CSRF-TOKEN。删除警报后会出现什么错误?也请从 ajax 中删除标头我更新了我的 ajax 你可以检查新的更新
  • 现在终于解决了。我自定义编码序列化函数以获取壁橱数据字符串作为 Jquery 序列化函数返回。无论如何感谢您的回答
【解决方案2】:

我检查了 laravel.log 文件的错误消息。发现 Jquery 序列化返回空字符串。因此,我自定义编码序列化函数以获取壁橱数据字符串作为 Jquery。

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

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2013-11-22
    • 1970-01-01
    相关资源
    最近更新 更多