【问题标题】:Fetch a valid url gives 404 error in node express获取有效的 url 在 node express 中给出 404 错误
【发布时间】:2020-07-21 14:30:25
【问题描述】:

我正在尝试使用 MEAN 堆栈构建 Web 应用,但获取请求存在问题。

app.route("/api/item/", function (req, res) {
    fetch("https://ddragon.leagueoflegends.com/cdn/10.1.1/data/en_US/item.json")
        .then(res => res.json())
        .then(data => {
            res.send({ data });
            // console.log(data);
        })
        .catch(err => {
            res.send(err);
        });
});

这是一个简单地获取 url 并获取 json 文件的 API。我通过使用 url 到 chrome 检查了 url,我可以看到 json 数据。

const httpOptions = {
  headers: new HttpHeaders({
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PATCH, DELETE, PUT, OPTIONS",
    "Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token",
    "Content-Type": "application/json"
  })
};

  getitem(): Observable<Item> {
    return this.http.get<Item>('http://localhost:8000/api/item/', httpOptions);
  }

我在 service.ts 中调用了 API

this.shservice.getitem().subscribe(im => {
      this.item = im;
    });

并在component.ts中订阅

由于我可以手动访问 url 并查看 json 数据,我认为不会有问题,但它一直给我 404 NOT FOUND 错误。这是一个非常简单的实现,因此更难找出哪个部分是错误的。

【问题讨论】:

    标签: node.js angular express fetch http-status-code-404


    【解决方案1】:

    您是否尝试过在 Node 中而不是从 Client 中添加 CORS?

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 2011-06-23
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      相关资源
      最近更新 更多