在laravel中发布请求到axios给出了500错误

8mmmxcuj  于 2023-10-18  发布在  iOS
关注(0)|答案(1)|浏览(133)

我有问题,在我的项目时,使用Axios的后方法,但当我使用得到方法在Axios和改变路线::后到路线::得到它实际工作。
错误是:请求失败,状态代码为500 ;
服务器错误是:注册?注册?full_name=&user_name=&email=&移动的=&password=&send=:64 POST http://192.168.1.4:8070/user/create 500(内部服务器错误)
我怎么能否认呢
Laravel中的路由:

Route::group(["as" => "user_", "prefix" => "user"], function () {
    Route::get('/signIn', [HomeController::class, 'signIn'])->name('signIn');
    Route::get('/signUp', [HomeController::class, 'signUp'])->name('signUp');
    Route::post("/create",[UserController::class , "store"])->name("create");
});

JavaScript语言:

<script>
    var getForms = document.getElementsByClassName('form-control');
    function submitForm(e) {
        axios.post('/user/create', {
            full_name : getForms[0],
            user_name : getForms[1],
            email : getForms[2],
            mobile : getForms[3],
            password : getForms[4],
        })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
        e.preventDefault();
    }
</script>

控制器

public function store(Request $request)
{
    $full_name = $request->full_name;
    $user_name = $request->user_name;
    $email = $request->email;
    $mobile = $request->mobile;
    $password = $request->password;

    $validator = Validator::make($request->all(), [
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'email' => 'required',
    ]);

    if ($validator->fails()) {
        return response()->json(['errors' => $validator->errors()], 422);
    }

    $user = userAuth::query()->create([
        "full_name" => $full_name,
        "user_name" => $user_name,
        "email" => $email,
        "mobile" => $mobile,
        "password" => $password,
    ]);

    return response()->json([
        'user' => $user,
        'message' => 'Success'
    ], 200);
}
yr9zkbsy

yr9zkbsy1#

我认为问题出在你的路线顺序上,请把post方法路线移到顶部,把get方法路线移到底部。我多次面对这个问题。
解决方案示例:

Route::group(["as" => "user_", "prefix" => "user"], function () {
    Route::post("/create",[UserController::class , "store"])->name("create");
    Route::get('/signIn', [HomeController::class, 'signIn'])->name('signIn');
    Route::get('/signUp', [HomeController::class, 'signUp'])->name('signUp');
});
  • 另外,尝试使用删除该高速缓存,*
php artisan optimize:clear

我希望这对你有帮助。

相关问题