简体   繁体   English

Axios的方法发布,给出错误404。ReactJS

[英]Method Post with Axios giving Error 404. ReactJS

i have a problem with axios.js , i trying to use the method POST but it give error 404 and i dont know what do, the error is: 我使用axios.js遇到问题,我尝试使用POST方法,但给出error 404 ,但我不知道该怎么办,错误是:

> POST http://localhost:3000/api/match 404 (Not Found)
> createError.js:16 Uncaught (in promise) Error: Request failed with status code 404
    at createError (createError.js:16)
    at settle (settle.js:18)
    at XMLHttpRequest.handleLoad (xhr.js:77)

My code with post : 我的代码与post

 teste = () => { console.log('login clicked') let data = JSON.stringify({ password: this.state.password, username: this.state.email }) axios.post('api/match', data, { headers: { 'Content-Type': 'application/json', } }) } 

Same thing if i to use this: 同样的事情,如果我使用这个:

 axios.post('http://localhost:3001/api/match', data, { headers: { 'Content-Type': 'application/json', } }) 

My package.json : 我的package.json

 "proxy": "http://localhost:3001/" 

Someone know why be giving this error? 有人知道为什么会给出这个错误? ???? ???? I look that it is giving the URL of the app React and not of the server.. 我看起来它提供的是应用程序React的URL,而不是服务器的URL。

Someone help me please? 有人帮我好吗?

The error says, basically, you don't have any http server running on localhost:3000 with a route /api/match. 该错误表明,基本上,您没有在带有路由/ api / match的localhost:3000上运行任何http服务器。

Are the http server running? http服务器正在运行吗?

Try changing api/match to /api/match in your first axios.post . 尝试改变api/match/api/match您的第一个axios.post A possible reason your second post request with the full URL isn't working might be a CORS issue. 您的第二个带有完整URL的发帖请求不起作用的可能原因可能是CORS问题。 There's a great cors package you can add to your backend app with two lines of code. 您可以使用两行代码将出色的cors软件包添加到后端应用中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM