I2mNew
I2mNew

Reputation: 173

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?

Upvotes: 1

Views: 3082

Answers (2)

I2mNew
I2mNew

Reputation: 173

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

Upvotes: 3

Yevhen Kazmirchuk
Yevhen Kazmirchuk

Reputation: 129

You have to configure proxy, you got CORS error.

if you are using create-react-app.

https://create-react-app.dev/docs/proxying-api-requests-in-development/

Upvotes: 0

Related Questions