我正在做一个基本的全栈来做应用程序。我用Java Spring做后端。当我用Postman测试我的后端时,它工作得很好,但当我用React fetch发送同样的请求时,它就不工作了!我想我取错了,但我不能理解。
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
我的问题是什么?
发布于 2021-11-06 19:28:41
我将"proxy": "http://localhost:8080"
添加到package.json中,并将获取url更改为"/auth/register"
,现在它可以工作了,谢谢!
发布于 2021-11-05 23:37:43
你必须配置代理,你得到了CORS错误。
如果您正在使用create-react-app。
https://create-react-app.dev/docs/proxying-api-requests-in-development/
https://stackoverflow.com/questions/69858492
复制相似问题