我的帖子请求得到 https 响应但不返回到新页面(javascript)

我发送了一个帖子请求,我得到了这样的回复


“ [符号(响应内部)]:{ 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));

};


当年话下
浏览 81回答 1
1回答

12345678_0001

以下是如何使用fetch()withasync/await语法:const login= () => async () => {&nbsp; const api = `somePostRequest`;&nbsp; const response = await fetch(api, {&nbsp; &nbsp; method: 'post',&nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; 'Content-Type': 'application/x-www-form-url-encoded',&nbsp; &nbsp; &nbsp; Accept: 'application/json',&nbsp; &nbsp; },&nbsp; });&nbsp; const data = await response.json(); // { url: 'https://login.somenewloginpage'}&nbsp; window.location.replace(data.url); // <-- Redirection};
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript