Search code examples
javascriptreactjsrequestfrontendfetch

Postman request works but my React request doesn't. Where is my mistake


I'm making a basic full stack to do app. I made backend with Java Spring. When I test the my backend with Postman it works well but when I send the same request with React fetch it doesn't work! I guess my fetch is wrong but I can't understand.

enter image description here

function register(){
fetch('http://localhost:8080/auth/register',
    {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(
            {
                'userName' : 'Pala',
                'password' : 'sibop',
            }
        )
    }
).then(
    res => {
        console.log("Result: " + res)
    }
).catch(
    err => {
        console.log("Erorororor: " + err)
    }
)}
function Home() {
return (
    <div>
        <h1>To Do!</h1>
        <form>
        <div id="signupArea">
            <h2>Name</h2>
            <input id="userName"/>
            <h2>Password</h2>
            <input id="password" type="password"/>
            <button onClick={register}>SIGN UP</button>
            <button>LOGIN</button>
        </div>
        </form>
    </div>
)}export default Home

enter image description here

What is my problem?


Solution

  • I added "proxy": "http://localhost:8080" to package.json and changed the fetch url to "/auth/register" and now it works, thanks!