繁体   English   中英

来自 Zomato api 的搜索查询 + 发布数据

[英]search query + post data from Zomato api

该站点上的另一个人能够帮助我了解如何从 jsonplaceholder 发布数据。 但是我试图弄清楚如何从 zomato api 发布数据,因为我想模拟一个执行搜索查询并根据输入发布数据的应用程序,但例如,如果我搜索“bethesda”,我不确定 url 是什么将需要获取。 文档中的参数是否都需要在 url 中? 只是对我将如何获得响应 ID 以及如何让它搜索我在搜索字段中输入的特定城市的所有餐馆感到困惑。

https://developers.zomato.com/documentation#!/common/establishments

    Description    Parameter Type    Data Type
     user-key    
    (required)

    your API key

    header               string
    city_id    
    id of the city

    query               integer
    lat    
    latitude / longitude of any point within a city

    query                double
    lon    
    latitude / longitude of any point within a city

    query                  double 
    var apiKey = 'd40175980577c1cb4df25d608886594b';
    var api = 'https://developers.zomato.com/api/v2.1/search?entity_id=259&entity_type=city&start=0&count=20'
    '
    var city = Bethesda;
    var url = api + city + apiKey;


function GetPosts(e) {
    e.preventDefault();
    fetch('https://jsonplaceholder.typicode.com/posts')
      .then((res) => res.json())
      .then((data) => {
        console.log(data);
        let output = '<h2>Get Posts</h2>';
        data.forEach(function(post) {

          output += `
              <div>
                  <h2>${post.title}</h2>
                  <p>${post.body}</p>

              </div>
              `;
        });
        document.getElementById('restaurant_results').innerHTML = output;

      })
  }
  document.querySelector('.submit-btn').addEventListener('click', GetPosts);

Zomato API 文档实际上会制作一个 cURL 请求,您可以复制以供您试用。 https://developers.zomato.com/documentation#!/restaurant/restaurant_0

在您的情况下,Zomato API 生成:

curl -X GET --header "Accept: application/json" --header "user-key: XXXXXX" "https://developers.zomato.com/api/v2.1/locations?query=bethseda&count=10"

You can't just append an API key as you did above you need to include it in the header using the proper Header key as defined in their API docs.

fetch('https://developers.zomato.com/api/v2.1/locations?query=bethseda&count=10', 
  {
  method: "GET",
  headers: {
    "Content-Type": "application/json",
     "Accept": "application/json",
    "user-key": "XXXXXXXXXXXX"
  }}).then(function(response){
     return response.json(); 
 }).then(function(data){
   console.log(data);
 });  

使用fetch文档: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

如果您希望能够添加一个值并更改位置,在您的情况下 chnage bethseda 那么您可以使 url 动态并更改城市名称,然后再将其传递给fetch

请注意方法是如何获取的,我如何传入标头,并将cURL请求转换为fetch

暂无
暂无

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

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