我发送了一个帖子请求,我得到了这样的回复“ [符号(响应内部)]:{ url:'https://login.somenewloginpage'}”我想做的是我想通过该网址打开一个新页面,但它不会定向到新页面。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贡献1802条经验 获得超5个赞
以下是如何使用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
};
添加回答
举报
0/150
提交
取消