简体   繁体   English

邮递员请求有效,但我的 React 请求无效。 我的错在哪里

[英]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.我用 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!当我使用 Postman 测试我的后端时,它运行良好,但是当我使用 React fetch 发送相同的请求时,它不起作用! I guess my fetch is wrong but I can't understand.我想我的提取是错误的,但我无法理解。

在此处输入图片说明

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

在此处输入图片说明

What is my problem?我的问题是什么?

You have to configure proxy, you got CORS error.您必须配置代理,出现 CORS 错误。

if you are using create-react-app.如果您使用的是 create-react-app。

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

我将"proxy": "http://localhost:8080"到 package.json 并将获取 url 更改为"/auth/register" ,现在它可以工作了,谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 POST请求在POSTMAN中有效,但在我的代码JS中无效 - POST request works in POSTMAN but not in my code JS 我将 jquery ajax 转换为纯 js ajax 请求的错误在哪里 - Where my mistake in converting jquery ajax to pure js ajax request 不正确的推送(也许)。 发布请求不起作用。 Postman 没有看到我的原始数据 - Incorrect push(maybe). Post request doesn't work. Postman doesn't see my raw data 角度http请求不起作用,但邮差工作 - angular http request doesn't work but on postman works GET 请求在 Postman 中工作,但它不能作为代码工作 - GET request works in Postman but it doesn´t work as code GET请求适用于Postman,但为什么它不能与ReactJS fetch一起使用? - GET request works with Postman but why doesn't it work with ReactJS fetch? 为什么我的帖子请求在 POSTMAN 中有效,但在我的反应应用程序中无效? - Why is my post request working in POSTMAN but not in my react application? 我的 GET 请求在 POSTMAN 和 Curl 中运行良好,但在 JavaScript 代码中运行良好 - My GET request works fine in POSTMAN and Curl, but not in JavaScript code 代码中我的对话流请求有什么问题(它适用于邮递员)? - What is the problem of my dialogflow request in the code (it works with postman)? React-使用axios的Nodejs / Express获取请求-无法从客户端访问我的后端路由-有什么错误? - React - Nodejs / express get request using axios - can't hit my backend route from client - what is the mistake?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM