【问题标题】:My url is working in Postman but Not working when using it from a browser?我的网址在 Postman 中有效,但在浏览器中使用时无效?
【发布时间】:2019-08-21 20:43:48
【问题描述】:

我希望你做得很好,我有一个小问题,我正在使用 Symfony 4 开发一个 API,并在前端使用 ReactJs,所以我使用 axios 将我的数据发送到服务器,这里是:

import Axios from "axios";

const sendLoginData = (username, password) => {
  Axios.post("localhost/api/login_check", { username, password }).then(
    response => {
      console.log(response);
    }
  );
};

export default sendLoginData;

数据发送正确,但似乎我的路线未知我在控制台中收到此错误:

xhr.js:166 发布http://localhost:3000/localhost/api/login_check 404 (未找到)

但是当我在 Postman 上使用相同的 url 时,我的令牌是正确的,这意味着我的身份验证很好。

所以,我验证了一些答案并找到了与我类似的东西,一个使用 .NET 作为后端的人并遇到了同样的错误,但据我所知,数据是通过 url 中的参数发送到服务器的,但是我相信我的情况不同。

我不知道如何解决这个问题,任何帮助将不胜感激。

任何帮助将不胜感激。

【问题讨论】:

  • 两件事:a.) ``` Axios.post("localhost/api...``` 或者(当前端和API的端口相同时 - ``` Axios.post( "/api/...``` b.) 未来(当前端端口和 API 不同时):API 是否返回 CORS 标头?
  • 您的 API 在哪个端口上运行?我的预感是 React 默认运行的不是 3000。
  • 你试过Axios.post("//api/login_check") 吗?
  • 尝试将Axios.post("localhost/api/login_check"更改为Axios.post("http://localhost/api/login_check"

标签: reactjs symfony axios api-platform.com


【解决方案1】:

在您的请求中添加方案。

在 postman 中,您请求 http://localhost/api/login_check,但在 axios 中,您请求 http://localhost/localhost/api/login_check

解决这个问题:

Axios.post("http://localhost/api/login_check")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 2021-04-03
    • 2016-07-11
    • 2020-07-31
    • 2021-11-27
    • 2021-09-19
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多