简体   繁体   English

为什么我的帖子请求 go 到不同的 url 然后得到请求

[英]Why does my post request go to a different url then get request

I'm having issues sending post requests.我在发送帖子请求时遇到问题。 Whenever I try and send a post request to localhost:5000/api it gives me this error "POST http://localhost:5000/api 404 (Not Found)" but whenever I do a get request it gives me no errors and works perfectly.每当我尝试向 localhost:5000/api 发送发布请求时,它都会给我这个错误“POST http://localhost:5000/api 404(未找到)”但是每当我发出 get 请求时,它都不会给我任何错误并且可以正常工作完美。

const createPost = async () => {
    let res = await axios.post("http://localhost:5000/api", {"id" : 4, "name" : "this was posted by code :)"})
    console.log(res)
  }

  useEffect(() => {
    axios.get("http://localhost:5000/api").then(res => {
      setBackendData(res.data)
    }).catch(err => {
      console.log(err)
    })
  }, [])

If you need anymore of my code please tell me.如果您需要我的代码,请告诉我。

Your backend has been written specifically without support for post.您的后端是专门编写的,不支持 post。 If you want, you could read this w3schools article on what different http methods exist.如果您愿意,您可以阅读w3schools文章,了解存在哪些不同的 http 方法。 In the modern age, it's all semantic so what api endpoints you choose is up to you (post is generally recommended for security reasons though).在现代,一切都是语义化的,因此您选择的 api 端点取决于您(但出于安全原因,通常建议使用帖子)。

If you want your backend to support post, change your code to use app.post instead of app.get .如果您希望您的后端支持 post,请将您的代码更改为使用app.post而不是app.get

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

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