首页 > 解决方案 > fetch 包无法从 API 中获取任何数据

问题描述

我正在开发一个 react js 项目,需要从API.

我正在使用fetch()来获取数据。这是我的代码。

fetch('https://sandboxapi.ampcome.com/api/gokhana/restaurant/categorized', {
  method: 'GET',
  headers: {
    'Authorization': 'WqQg2jqLWVIO5JBmQ1LYHU8w41FdzLEsCB6yP1Tw2AFHf2w5KWRkMBV8KAgY6Kl5'
  },
  qs: {
    geopoint: {
      near: {
        lat: 12.9699,
        lng: 77.6499
      },
      maxDistance: 50,
      unit: 'kilometers'
    },
    categories: ['trending', 'newaddition', 'popular'],
    outlettypes: ['restaurant', 'foodcourt'],
    clientapp: 'gokhanacustomer'
  }
})
  .then(res => res.json())
  .then(res => console.log(res));

一切都很好,但这会返回空数组。有很多数据,但它返回一个空数组。

我尝试了同样的axios方法,它获取数据,我无法找出确切的问题fetch()

我需要比较axios和获取的工作,这就是为什么我需要通过两种方式测试它。

标签: reactjsapifetchaxios

解决方案


推荐阅读