【问题标题】:How to change port using fetch and request react js?如何使用 fetch 和 request react js 更改端口?
【发布时间】:2020-11-24 02:47:21
【问题描述】:

这是我从教授的教程中获取的代码。 LoginComp 只是 {用户名:'',密码:''}

const request = new Request("/users/login", {
    method: "post",
    body: JSON.stringify(loginComp),
    headers: {
        Accept: "application/json, text/plain, */*",
        "Content-Type": "application/json"
    }
});

// Send the request with fetch()
fetch(request)
    .then(res => {
        if (res.status === 200) {
            return res.json()


        }
    })

    .catch(error => {
        console.log(error);
    });

我有一个在 localhost:5000 运行的后端服务器,在 localhost:5000/users/login 有发布路由。这段代码的问题是它假设用户/登录是在 localhost 3000 所以我会在控制台中得到这个错误:

POST http://localhost:3000/users/login 404 (Not Found)

如何使它的 localhost:5000 而不是 3000(客户端服务器)

【问题讨论】:

    标签: node.js reactjs express fetch


    【解决方案1】:

    您必须将 http://localhost:5000/ 添加到您的网址。

    const request = new Request("http://localhost:5000/users/login", {
        method: "post",
        body: JSON.stringify(loginComp),
        headers: {
            Accept: "application/json, text/plain, */*",
            "Content-Type": "application/json"
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 1970-01-01
      • 2015-12-20
      • 2018-05-10
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 2017-03-19
      相关资源
      最近更新 更多