【问题标题】:Reading RapidAPI JSON response using fetch method使用 fetch 方法读取 RapidAPI JSON 响应
【发布时间】:2020-02-05 13:00:21
【问题描述】:

我正在开发一个用于学习目的的 react-native 项目。我也在使用 RapidAPI (https://rapidapi.com/divad12/api/numbers-1/endpoints)。

当我点击 API 时,我的状态为 200OK,但我无法从 API 读取 JSON 格式的响应数据。

代码:

fetchCurrencyData = () => {
    fetch("https://numbersapi.p.rapidapi.com/7/21/date?fragment=true&json=true", {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "numbersapi.p.rapidapi.com",
            "x-rapidapi-key": "<Valid API Key, generated in code snippet>"
        }
    })
    .then(response => {         
        console.log(response);
    })
    .catch(err => {
        console.log(err);
    }); 
}

componentDidMount(){
    this.fetchCurrencyData();
}

在 console.log(response);我明白了:

我检查了 RapidAPI -> MyApps 部分中的响应:

如何读取 JSON 格式的响应正文?

【问题讨论】:

    标签: javascript json api react-native fetch


    【解决方案1】:

    当前您正在打印响应对象,其中包含原始响应,包括标头等。 您可以执行以下操作:

    fetchCurrencyData = () => {
        fetch("https://numbersapi.p.rapidapi.com/7/21/date?fragment=true&json=true", {
            "method": "GET",
            "headers": {
                "x-rapidapi-host": "numbersapi.p.rapidapi.com",
                "x-rapidapi-key": "<Valid API Key, generated in code snippet>"
            }
        })
        .then(response => response.json()) // Getting the actual response data
        .then(data => {         
            console.log(data);
        })
        .catch(err => {
            console.log(err);
        }); 
    }
    
    componentDidMount(){
        this.fetchCurrencyData();
    }
    

    【讨论】:

      【解决方案2】:

      我有同样的问题。从 RapidApi 嵌入 spoonacular API 时。然后我用了这个:

          <?php
          $headers = array('Accept' => 'application/json');
          $response = Unirest\Request::get("https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/search?number=50&query='.$plus_separated.'",
            array(
              "X-RapidAPI-Key" => "bc038c4c88mshae2640d....e1acp1301aejsnd5703df78862"
            )
          );
              $array_1 = $response->raw_body;         
          }
          ?>
      
          <div id="result_body" class="container">
                 <P id="allert_msg" class="text-center">Results Not Found</P>
          </div>
      
          <script>
                  var array_2 = '<?php echo  $array_1;?>';
                  var array_3 = JSON.parse(array_2);
                  var main_array = array_3.results;
                  var main_array_length = main_array.length;
                  var i=j=k=l=m=0;
      
                  for(i=0; i < main_array_length; i++){
                  var result_body= document.getElementById("result_body");
                  var body_link = document.createElement("a");
                  body_link.setAttribute("class", "single_link");
                  body_link.setAttribute("target", "_blank");
                  body_link.setAttribute("href", 'recipe_details.php?id='+main_array[i].id+'&submit_id=OK');
                  result_body.appendChild(body_link);
      
                  var p = document.createElement("div");
                  p.setAttribute("id", "single_item_"+j++);
                  p.setAttribute("class", "single_item");
                  body_link.appendChild(p);
          </script>
      

      这是用于食谱搜索的目的。代码自动创建 div、a、p、... 等 HTML DOM 元素并显示搜索结果。您应该将正确的值放在正确的位置,它来自响应,以 json 格式。在上面,我将响应放在 PHP 中的“$array_1”中,然后放在 Javascript 中的“array_2”中。然后用 Javascript 继续下一个过程。 如果你在我的代码中有一些错误,并且有更好的主意,请告诉我。谢谢!

      【讨论】:

        猜你喜欢
        • 2020-07-21
        • 1970-01-01
        • 1970-01-01
        • 2017-09-06
        • 1970-01-01
        • 2023-03-18
        • 2022-01-09
        • 1970-01-01
        • 2020-12-19
        相关资源
        最近更新 更多