javascript - How to redirect user to a page after receiving the response from a fetch POST request? - Stack Overflow

I am writing code for a web application that send a POST request to node.js server using fetch() api of

I am writing code for a web application that send a POST request to node.js server using fetch() api of javascript. On successful request server responds with a redirection, this redirection url is received in the fetch() api response body. After this what should I do to redirect user to this URL from fetch function.

fetch("/events/registration", {
          method: "POST",
          redirect:"follow",
          headers: {
            "Accept": "application/json , text/plain ,*/*",
            "Content-type": "application/json"
          },
          body: JSON.stringify({
            name,
            email,
          })
        })
.then((res)=>res.json())
.then((data)=>{console.log(data);if(data.err)alert(data.err);Response.redirect()});

In response that i receive in fetch api I am getting redirect:true , url: "LinkOfRedirection/redirect", now what should i do to redirect user from here to the LinkOfRedirection/redirect.

I am writing code for a web application that send a POST request to node.js server using fetch() api of javascript. On successful request server responds with a redirection, this redirection url is received in the fetch() api response body. After this what should I do to redirect user to this URL from fetch function.

fetch("/events/registration", {
          method: "POST",
          redirect:"follow",
          headers: {
            "Accept": "application/json , text/plain ,*/*",
            "Content-type": "application/json"
          },
          body: JSON.stringify({
            name,
            email,
          })
        })
.then((res)=>res.json())
.then((data)=>{console.log(data);if(data.err)alert(data.err);Response.redirect()});

In response that i receive in fetch api I am getting redirect:true , url: "LinkOfRedirection/redirect", now what should i do to redirect user from here to the LinkOfRedirection/redirect.

Share edited Mar 18, 2022 at 9:20 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Jan 29, 2019 at 5:05 Ayush AgrawalAyush Agrawal 691 gold badge1 silver badge5 bronze badges 2
  • stackoverflow./questions/11355366/… it may help you – Rahul Dudharejiya Commented Jan 29, 2019 at 5:08
  • Please have a look at this answer – Chris Commented Apr 7, 2023 at 17:06
Add a ment  | 

2 Answers 2

Reset to default 1
// Sets the new location of the current window.
window.location = res.url;

// Sets the new href (URL) for the current window.
window.location.href = res.url;

// Assigns a new URL to the current window.
window.location.assign(res.url);

// Replaces the location of the current window with the new one.
window.location.replace(res.url);

// Sets the location of the current window itself.
self.location = res.url;

// Sets the location of the topmost window of the current window.
top.location = res.url;

Or you can use

res.redirect(301, res.url);

A simple solution to this would be something like this:

fetch('/api/login/' , { method : 'POST', headers:{ 'Content-Type':'application/json', 'X-CSRFToken' : csrf, },

    body : JSON.stringify(data)
}).then(result => result.json())
.then(response => {
    
    if(response.status == 200){
        window.location.href = '/'
    }
    else{
        alert(response.message)
    }

})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745061740a4609007.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信