亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 fetch vanilla JS 傳遞可選參數

如何使用 fetch vanilla JS 傳遞可選參數

繁星點點滴滴 2023-06-15 17:38:55
我正在使用 JavaScript fetchGET 方法來調用 API。API返回數據;但是,我想傳遞一些可選參數以不同的方式格式化數據響應。如何使用該fetch方法傳遞可選參數?async function getText(){        let passageParam = randomPassage();    //API credit    let Url = 'https://api.esv.org/v3/passage/text?q=' + passageParam + params;    console.log(Url);    //Await - Used with Async    //Suspend function exeeuction until the Async promise settles and returns its result    let response = await fetch(Url, {        method: 'GET',        headers: {            'Authorization': 'myToken'         },        params = {            'indent-poetry': False,            'include-headings': False,            'include-footnotes': False,            'include-verse-numbers': False,            'include-short-copyright': False,            'include-passage-references': False        }    });    if(response.ok){ // if HTTP-status is 200-299        // get the response body        let passage = await response.json();                populateUI(passageParam, passage.passages[0]);        //console.log(passage);     } else{        alert("HTTP-Error: " + response.status);     }     //Function to input json response to HTML     function populateUI(ref, verse){        //strip verse        document.getElementById('reference').innerHTML = ref;        document.getElementById('verse').innerHTML = verse;    }}
查看完整描述

2 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

使用fetchwith時,通常期望通過查詢字符串GET發送參數。

你可以嘗試這樣的事情:


let passageParam = randomPassage();

let extraParams = '&indent-poetry=False&include-headings=False' +

? ? '&include-footnotes=False&include-verse-numbers=False' +?

? ? '&include-short-copyright=False&include-passage-references=False';

let Url = 'https://api.esv.org/v3/passage/text?q=' + passageParam + extraParams;

console.log(Url);

或者你可以這樣做:


let passageParam = randomPassage();

let extraParams = {

? ? 'indent-poetry': 'False',

? ? 'include-headings': 'False',

? ? 'include-footnotes': 'False',

? ? 'include-verse-numbers': 'False',

? ? 'include-short-copyright': 'False',

? ? 'include-passage-references': 'False'

}

let Url = 'https://api.esv.org/v3/passage/text?q=' + passageParam +?

? ? '&' + (new URLSearchParams(extraParams)).toString();

console.log(Url);

并刪除params表達式。



查看完整回答
反對 回復 2023-06-15
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

由于您正在使用fetchURL-EndPoint發出GET請求。每次調用 URL-EndPint 都會返回相同的數據格式。

在這種情況下,格式化響應不在我們手中。要檢查所有響應詳細信息,請轉到 Developer Console網絡選項卡(執行Ctrl+Shift+I),您可以看到響應中收到的響應標頭和其他相關內容,看看是否有任何信息對您有用那里本身。


查看完整回答
反對 回復 2023-06-15
  • 2 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號