简体   繁体   English

axios.post在应用程序中失败,在URL中工作

[英]axios.post Fails in App, Works in URL

I'm following a React-Redux tutorial. 我正在关注React-Redux教程。 My axios.post() is failing when running the app, but if I use that same URL and paste it into the browser's address textbox it works. 我的axios.post()在运行应用程序时失败,但是如果我使用相同的URL并将其粘贴到浏览器的地址文本框中则可以正常工作。 Why would that happen? 为什么会这样?

Here is the call in my app: 这是我的应用程序中的调用:

const request = axios.post(`${ROOT_URL}/posts/${id}${API_KEY}`);

Here is the error, as shown in F12 in Chrome: 这是错误,如Chrome中的F12所示:

POST http://reduxblog.herokuapp.com/api/posts/120342?key=bob884 404 (Not Found) POST http://reduxblog.herokuapp.com/api/posts/120342?key=bob884 404(未找到)

Why would that fail when it's a good URL? 当它是一个好的URL时,为什么会失败? If you click it, you'll see the response in the browser: 如果单击它,您将在浏览器中看到响应:

{"id":120342,"title":"SOLID","categories":"OOP","content":"SOLID is an acronym..."} {“id”:120342,“title”:“SOLID”,“categories”:“OOP”,“content”:“SOLID是首字母缩略词...”}

When you click the link, the browser sends a GET request to the server. 单击该链接时,浏览器会向服务器发送GET请求。
A POST endpoint might not be available from the server side at that specific URL address. 在该特定URL地址的服务器端可能无法使用POST端点。

Whenever the server is unable to find a URL with a specified method ( GET / POST ), it returns a 404 - Not found error. 只要服务器无法找到具有指定方法( GET / POST )的URL,它就会返回404 - Not found错误。 In this case, it doesn't find any POST method defined for that address. 在这种情况下,它找不到为该地址定义的任何POST方法。

You should consider changing the method to a GET request, if that's what you desire. 您应该考虑将方法更改为GET请求,如果这是您想要的。

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

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