首页 > 解决方案 > axios 请求忽略我的代理,即使硬编码我也无法获取任何数据

问题描述

我正在尝试从 API 获取图像并通过它们进行映射,但我的 axios 忽略了我的 package.json 中的代理。

componentDidMount() {
const { count, start } = this.state;
axios
  .get(`/api/photos?count=${count}&start=${start}`)
  .then(res => this.setState({ images: res.data }));

}

即使我这样编码,我什么也得不到

componentDidMount() {
const { count, start } = this.state;
axios
  .get(`http:localhost:5000/api/photos?count=${count}&start=${start}`)
  .then(res => this.setState({ images: res.data }));

}

当我通过邮递员点击它时,我的 server.js 可以工作,所以我确定问题出在我的反应上,但我发布它是为了增加上下文

const unsplash = new Unsplash({
  applicationId: config.get("APPLICATION_ID"),
  secret: config.get("SECRET"),
  callbackUrl: config.get("CALLBACK_URL"),
});

const app = express();
app.use(cors())
app.get("/api/photos", (req, res) => {
  unsplash.photos
    .listPhotos(req.query.start, req.query.count)
    .then(toJson)
    .then((json) => res.json(json));
});

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

标签: reactjsexpressaxios

解决方案


推荐阅读