我想將 API 調(diào)用(外部源,沒(méi)有機(jī)會(huì)在 API 端更改某些內(nèi)容)從 PHP 更改為 Javascript(學(xué)習(xí)目的)。由于跨域,我使用 fetch()。當(dāng)我運(yùn)行我的腳本時(shí),我收到一個(gè)意外的輸入錯(cuò)誤結(jié)束并且無(wú)法弄清楚原因。function postData(url = '', data = {}) { var headers = new Headers(); headers.set('Authorization', 'Basic ' + window.btoa("user" + ':' + "pass")); return fetch(url, { method: 'POST', mode: 'no-cors', cache: 'no-cache', credentials: 'include', headers: { 'Content-Type': 'application/json' }, redirect: 'follow', referrer: 'no-referrer', body: JSON.stringify(data), }).then(response => response.json()).catch(error => console.error(error));}postData('https://www.api-endpoint.com/cat1/api/search?', { "searchID": "710", "isTagged": true}).then(data => console.log(JSON.stringify(data))).catch(error => console.error(error));如何識(shí)別此代碼的問(wèn)題?看來(lái)授權(quán)沒(méi)問(wèn)題。我按照 API Dev 的手冊(cè)中的描述實(shí)現(xiàn)了搜索參數(shù)(searchID 和 isTagged)。
通過(guò) fetch() 調(diào)用 API - 意外的輸入結(jié)束
富國(guó)滬深
2021-09-30 13:57:33