![](/img/trans.png)
[英]How to use ContextualWeb News API in node.js using axios HTTP client?
[英]How to embed ContextualWeb News API in an HTML page?
我試圖將ContextualWeb News API嵌入到一個簡單的HTML頁面中。 按下按鈕后,新聞API應該返回結果列表。 我想將響應打印到控制台。
該請求看起來像這樣:(但是它們沒有提供完整的HTML示例)
const url ="https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI?autoCorrect=false&pageNumber=1&pageSize=10&q=Taylor+Swift&safeSearch=false"
const options = {
method: 'GET',
headers: {
"X-RapidAPI-Host": "contextualwebsearch-websearch-v1.p.rapidapi.com",
"X-RapidAPI-Key": "XXXXXXXX"
},
}
fetch(url, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(e => console.error(e))
可以在此處獲取Rapid API密鑰: https : //rapidapi.com/contextualwebsearch/api/web-search
想要一個帶有按鈕的HTML,並將結果輸出到控制台或文本框。
您可以嘗試如下操作:
fetch(url, options)
.then(response => response.json())
.then(data => showResults(data))
.catch(e => console.error(e))
showResults(data) {
data.map(news => console.log(news.title));
}
在提取中調用將處理結果的函數。 如果您使用的是純JavaScript,則可以嘗試innerHTML編寫結果。
我能夠弄清楚。 這是嵌入在簡單HTML頁面中的ContextualWeb News API請求。 按下按鈕后,結果JSON將寫入控制台。
<!doctype html>
<html>
<head>
</head>
<body>
<div style="margin: 40px 40px; width: 450px;">
<button onclick="makeGETRequest()">Make the News API call</button>
<p>see console the for results</p>
</div>
<script>
function makeGETRequest() {
const url = "https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI?autoCorrect=false&pageNumber=1&pageSize=10&q=Taylor+Swift&safeSearch=false"
const options = {
method: 'GET',
headers: {
"X-RapidAPI-Host": "contextualwebsearch-websearch v1.p.rapidapi.com",
"X-RapidAPI-Key": "XXXXXXXXXXXXXXX"
},
}
fetch(url, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(e => console.error(e))
}
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.