繁体   English   中英

如何在 React JS 中使用 Zomato API

[英]How to use Zomato API with React JS

我正在尝试使用 Zomato API 来搜索餐厅,现在我正在尝试搜索 console.log 一些数据。 Zomato文档中,它给了我这个 Curl 和 Request URL

curl -X GET --header "Accept: application/json" --header "user-key: a725a13c0e61675a1eb07e3df050cd20" "https://developers.zomato.com/api/v2.1/search"

https://developers.zomato.com/api/v2.1/search

我不确定如何实现这一点。 到目前为止我尝试做的是这个

    const getResturants = () => {
  const axios = require('axios');
  axios({
    method: 'GET',
    url: 'https://developers.zomato.com/api/v2.1/search',
    headers: {
      "user-key": "a725a13c0e61675a1eb07e3df050cd20",
      "content-type": "application/json"
    }
  })
    .then(response => {
      console.log(response.data.restaurants[0].restaurant.name);
    }).catch(error => {
      console.log(error);
    })
}

但是我收到一条错误消息。

我怀疑的一点是一天允许 1000 个请求,当它超过限制时,请求将被限制。 因此请确保不超过请求计数。 我使用下面的代码通过查找纬度和经度来获取餐厅并且它有效。 下面是我从 zomato api 获取结果的代码。

sendGetRestaurant = async () => {
    try {
        const resp = await axios.get('https://developers.zomato.com/api/v2.1/search?lat='+this.state.Latitude+'&lon='+this.state.Longitude,{
            headers: {"user-key": "Access Key"}
          });
        console.log(resp.data);
    } catch (err) {
        console.error(err);
    }
}

下面是获取经度和纬度的代码。

 getLocation() {
        navigator.geolocation.getCurrentPosition(
            (position) => {
                console.log("Latitude is :", position.coords.latitude);
                console.log("Longitude is :", position.coords.longitude);
                this.setState({
                    Latitude: position.coords.latitude,
                    Longitude: position.coords.longitude
                })
                this.sendGetRestaurant();
            },
            function (error) {
                console.error("Error Code = " + error.code + " - " + error.message);
            },
            {
                enableHighAccuracy: false,
                timeout: 10000,
                maximumAge: 10000
            }

        );
    }

然后我在 componentDidMount 方法中调用了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM