當(dāng)年話下
2023-07-29 14:50:37
我發(fā)送了一個(gè)帖子請(qǐng)求,我得到了這樣的回復(fù)“ [符號(hào)(響應(yīng)內(nèi)部)]:{ url:'https://login.somenewloginpage'}”我想做的是我想通過該網(wǎng)址打開一個(gè)新頁面,但它不會(huì)定向到新頁面。const login= () => async () => { const api = `somePostRequest` fetch(api, { method: 'post', headers: { 'Content-Type': 'application/x-www-form-url-encoded', Accept: 'application/json', }, }) .then(function(res) { return res //maybe I should do something in this part... }) .then(data => console.log(data));};
1 回答

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
以下是如何使用fetch()withasync/await語法:
const login= () => async () => {
const api = `somePostRequest`;
const response = await fetch(api, {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-url-encoded',
Accept: 'application/json',
},
});
const data = await response.json(); // { url: 'https://login.somenewloginpage'}
window.location.replace(data.url); // <-- Redirection
};
添加回答
舉報(bào)
0/150
提交
取消