Alewa's avatar
Level 2

POST http://localhost:3000/api/register 500 (Internal Server Error) Laravel 11 api with react.js vite error

I am having this two error codes POST http://localhost:3000/api/register 500 (Internal Server Error) and this error also Uncaught (in promise) SyntaxError: Unexpected end of JSON input , any help? vite.config.js file ` import { defineConfig } from "vite"; import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], server: { proxy: { "/api": { target: "http://127.0.0.1:8000", // target: "http://ghtechs", changeOrigin: true, headers: { Accept: "application/json", "Content-Type": "application/json", }, }, }, }, }); register.jsx file import { useContext, useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { AppContext } from "../../Context/AppContext";

export default function Register() { const navigate = useNavigate(); const { token, setToken } = useContext(AppContext);

const [formData, setFormData] = useState({
    name: "",
    email: "",
    password: "",
    password_confirmation: "",
});

const [errors, setErrors] = useState({});

async function handleRegister(e) {
    e.preventDefault();
    const res = await fetch("/api/register", {
        method: "post",
        body: JSON.stringify(formData),
    });

    const data = await res.json();

    if (data.errors) {
        setErrors(data.errors);
    } else {
        localStorage.setItem("token", data.token);
        setToken(data.token);
        navigate("/dashboard");
        console.log(data);
    }
}

return (
    <>
        <div className="pt-[80px]">
            <div className="mx-auto w-[300px] border border-gray-300 items-center justify-center rounded p-2">
                <h1 className="text-[20px] text-blue-600 font-semibold text-center pb-[15px]">
                    Create Your Account
                </h1>
                <form onSubmit={handleRegister}>
                    <div className="pb-3">
                        <input
                            type="text"
                            className="w-full border border-gray-300 pl-1 py-1 focus:border-blue-400 focus:ring-0"
                            placeholder="Enter Name"
                            value={formData.name}
                            onChange={(e) =>
                                setFormData({
                                    ...formData,
                                    name: e.target.value,
                                })
                            }
                        />
                        {errors.name && (
                            <p className="text-red-600">{errors.name[0]}</p>
                        )}
                    </div>
                    <div className="pb-3">
                        <input
                            type="email"
                            className="w-full border border-gray-300 pl-1 py-1 focus:border-blue-400 focus:ring-0"
                            placeholder="Enter Email"
                            value={formData.email}
                            onChange={(e) =>
                                setFormData({
                                    ...formData,
                                    email: e.target.value,
                                })
                            }
                        />
                        {errors.email && (
                            <p className="text-red-600">
                                {errors.email[0]}
                            </p>
                        )}
                    </div>
                    <div className="pb-3">
                        <input
                            type="password"
                            className="w-full border border-gray-300 pl-1 py-1 focus:border-blue-400 focus:ring-0"
                            placeholder="Enter Password"
                            value={formData.password}
                            onChange={(e) =>
                                setFormData({
                                    ...formData,
                                    password: e.target.value,
                                })
                            }
                        />
                        {errors.password && (
                            <p className="text-red-600">
                                {errors.password[0]}
                            </p>
                        )}
                    </div>
                    <div className="pb-3">
                        <input
                            type="password"
                            className="w-full border border-gray-300 pl-1 py-1 focus:border-blue-400 focus:ring-0"
                            placeholder="Confirm Password"
                            value={formData.password_confirmation}
                            onChange={(e) =>
                                setFormData({
                                    ...formData,
                                    password_confirmation: e.target.value,
                                })
                            }
                        />
                    </div>
                    <div className="items-center pb-3">
                        <button className="w-full border border-blue-400 bg-blue-400 text-white py-1 hover:bg-blue-600 hover:border-blue-600">
                            REGISTER
                        </button>
                    </div>
                    <div className="pb-3">
                        <Link
                            to="/login"
                            className="text-gray-400 hover:text-blue-600"
                        >
                            Don't have an account?
                        </Link>
                    </div>
                </form>
            </div>
        </div>
    </>
);

}

But when i try to call my register api from laravel the server callsPOST http://localhost:3000/api/register 500 (Internal Server Error), shouldn't it rather be POST http://localhost:8000/api/register 500 (Internal Server Error), or? the localhost port is 3000 instead of 8000 or? and the errors is in my register.jsx filee.preventDefault(); const res = await fetch("/api/register", { method: "post", body: JSON.stringify(formData), });and the other is const data = await res.json();any help? And am getting[vite] http proxy error: /api/registerandError: connect ECONNREFUSED 127.0.0.1:8000 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16)` errors in my vite server.

0 likes
7 replies
puklipo's avatar

The 500 error doesn't contain any information. Check your Laravel log files.

martinbean's avatar

@Alewa A 500 error is a server side error. Your PHP code is throwing an exception somewhere. You need to actually look in your storage/logs/laravel.log file to see what the actual error is, and fix it.

Alewa's avatar
Level 2

@martinbean I am having this error

JussiMannisto's avatar

@Alewa Why did you cut out the actual error message? The log will show an error message and the stack trace. You only showed the latter.

1 like
Alewa's avatar
Level 2

api.php route file

Please or to participate in this conversation.