Javascript Zomato API不工作-ReactJs

Javascript Zomato API不工作-ReactJs,javascript,reactjs,api,zomato-api,Javascript,Reactjs,Api,Zomato Api,在reactjs中,我很难让一个基本的ZomatoAPI请求工作 api文档看起来很简单。我正在对类别执行基本的GET请求: 下面是我的ReactJS函数的样子: componentDidMount() { // A previous request for london restaurants // axios.get('https://developers.zomato.com/api/v2.1/geocode? lat=51.5138&lon=0.0984')

在reactjs中,我很难让一个基本的ZomatoAPI请求工作

api文档看起来很简单。我正在对类别执行基本的GET请求:

下面是我的ReactJS函数的样子:

  componentDidMount() {
   // A previous request for london restaurants 
  //  axios.get('https://developers.zomato.com/api/v2.1/geocode? 
 lat=51.5138&lon=0.0984')

 axios.get('https://developers.zomato.com/api/v2.1/categories')
  .then(res => {
    console.log(res.data);
    this.setState(
      { places: res.data});
  });
 }
然而,当我在浏览器中点击任何api url时,我都会得到这种响应。或者通过失眠

{
"code": 403,
"status": "Forbidden",
"message": "Invalid API Key"
}
我知道上面说API无效。但我是在登录Zomato的开发者门户并应用任何API密钥之后得到这些URL的。找不到任何指示来说明我哪里出了错

谢谢,
Reena。

您需要在请求头中设置此键

X-Zomato-API-Key:

样本请求:

curl -X GET --header "Accept: application/json" --header "X-Zomato-API-Key: <YOUR_API_KEY>" "https://developers.zomato.com/api/v2.1/categories"
curl-xget--header“Accept:application/json”--header“X-Zomato-API-Key:”https://developers.zomato.com/api/v2.1/categories"

希望这对您有所帮助。

您需要在请求头中设置此键

X-Zomato-API-Key:

样本请求:

curl -X GET --header "Accept: application/json" --header "X-Zomato-API-Key: <YOUR_API_KEY>" "https://developers.zomato.com/api/v2.1/categories"
curl-xget--header“Accept:application/json”--header“X-Zomato-API-Key:”https://developers.zomato.com/api/v2.1/categories"

希望这将对您有所帮助。

您似乎缺少
用户密钥
参数及其值。例如,URL应该类似于:

https://developers.zomato.com/api/v2.1/categories?user-key=<your API key>
https://developers.zomato.com/api/v2.1/categories?user-钥匙=

您似乎缺少
用户键
参数及其值。例如,URL应该类似于:

https://developers.zomato.com/api/v2.1/categories?user-key=<your API key>
https://developers.zomato.com/api/v2.1/categories?user-钥匙=

我明白了,答案是:

const config = { headers: {'user-key': 'MY KEY HERE'} }; 
enter code here
axios.get('developers.zomato.com/api/v2.1/…;, config) .then(res => { 
console.log(res.data.collections); this.setState( { places: 
res.data.collections }); }); 

谢谢大家

我明白了,答案是:

const config = { headers: {'user-key': 'MY KEY HERE'} }; 
enter code here
axios.get('developers.zomato.com/api/v2.1/…;, config) .then(res => { 
console.log(res.data.collections); this.setState( { places: 
res.data.collections }); }); 

谢谢大家

从标头传递API_键,您将获得响应数据

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

从标头传递API_键,您将获得响应数据

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

好的,但是我会用axios写这篇文章给谁呢?我不认为我在这个项目中使用curl…在您的axios请求中,只需在标题中设置“X-Zomato-API-Key:”,我得到了答案:const config={headers:{'user-Key':'MY Key HERE'};然后(res=>{console.log(res.data.collections);this.setState({places:res.data.collections});});谢谢大家。好的,但是我用axios写这封信给谁呢?我不认为我在这个项目中使用curl…在您的axios请求中,只需在标题中设置“X-Zomato-API-Key:”,我得到了答案:const config={headers:{'user-Key':'MY Key HERE'};然后(res=>{console.log(res.data.collections);this.setState({places:res.data.collections});});谢谢大家。