嘿,我不明白為什么這不起作用。我收到兩個錯誤。第一個是 value 不是函式,第二個是“catch(error)”之后的 '{' 是一個意外錯誤。當我將javascript直接添加到我的html中時,我沒有收到'{'錯誤。感謝您的幫助。該腳本包含在腳本中,但是當我輸入它時它會與代碼塊混淆。
const value = async() => {
const input = $(input);
try{
const response = await fetch(`https://wordsapiv1.p.rapidapi.com/words/hatchback/typeOf?=${input}`, {
method: 'GET',
headers{
'X-RapidAPI-Host': 'wordsapiv1.p.rapidapi.com',
'X-RapidAPI-Key': '6bf945be36msh9deedbbf5135343p16d366jsnd17d03ee5bd4'
}
})
const jsonResponse = response.json();
alert(`${jsonResponse}`)
} catch(error) {
console.log(error);
}
};
<input type="text" placeholder="enter location" id="input">
<button onclick="value()"></button>
uj5u.com熱心網友回復:
你應該像這樣更正你的代碼
var value = async() => {
const input = $("#input").val();
try {
const response = await fetch(
`https://wordsapiv1.p.rapidapi.com/words/hatchback/typeOf?location=` input, {
method: 'GET',
headers: {
'X-RapidAPI-Host': 'wordsapiv1.p.rapidapi.com',
'X-RapidAPI-Key': '6bf945be36msh9deedbbf5135343p16d366jsnd17d03ee5bd4'
}
})
const jsonResponse = response.json();
alert(`${jsonResponse}`)
} catch (error) {
console.log(error);
}
}
幾件事你錯過了:之前的標題宣告和輸入選擇器是錯誤的
uj5u.com熱心網友回復:
似乎輸入選擇器缺少'and #。您的輸入有一個 ID,其值為input
這不會起作用,因為它試圖選擇一個元素的值input
const input = $(input);
這里我們用 ID ( #) 輸入 ( #input)選擇和元素
const input = $('#input');
要獲取元素的值,只需使用.val()
const input = $('#input').val();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448244.html
標籤:javascript html api
上一篇:Azure前門和自定義域
