简体   繁体   English

重定向后重新加载页面

[英]Reloading the page after redirect

In my application I have a link for logout. 在我的应用程序中,我有一个注销链接。

html

<a onclick="myFunction()">Logout</a>

js

function myFunction() {
    document.location.href = 'https://url/oauth/logout?redirect=https://url';
}

The problem I am having is, when I click logout, I immediately redirects to the url . 我遇到的问题是,当我单击注销时,我立即重定向到url It seems like I am not logged out but after I refresh manually, I get logged out. 好像我没有注销,但是手动刷新后,我注销了。

I also tried window.reload(); 我也尝试过window.reload(); inside myFunction() but it actually doesn't get called at all. myFunction()内部,但实际上根本没有被调用。

Is there a way to reload the page after the myFunction() is called. 有没有一种方法可以在调用myFunction()之后重新加载页面。

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

You could fetch the logout endpoint and then reload the page if it was successful like so: 您可以获取注销端点,然后成功重新加载页面,如下所示:

const myFunction = async (event) => {
  event.preventDefault()
  const response = await fetch('https://url/oauth/logout');
  if (response.ok) {
    location.replace('new-url');
    // or location.reload()
  }
}

Instead of doing a "client side" redirect, try a "server side" redirect in whatever language you are using. 与其执行“客户端”重定向,不如尝试使用您使用的任何语言进行“服务器端”重定向。

<?php
    Logout Logic here
    header("Location: /login_url");
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM