2 回答

TA貢獻1784條經驗 獲得超9個贊
當前您正在打印響應對象,其中包含原始響應,包括標題等。您可以執行以下操作:
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();
}

TA貢獻1827條經驗 獲得超4個贊
我遇到過同樣的問題。從 RapidApi 嵌入勺子 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 繼續下一個過程。如果您在我的代碼中有一些錯誤,并且有更好的主意,請讓我知道。謝謝!
添加回答
舉報