PHP- Ajax and Redirect

前端 未结 3 627
情书的邮戳
情书的邮戳 2020-12-11 12:33

i have a jquery Ajax request happening on a page. On php side i am checking if the session is active and doing something. If the session is not active i want to redirect th

3条回答
  •  青春惊慌失措
    2020-12-11 13:06

    If I understand what you want to happen then this is how I'm implementing it. It's in Prototype instead of jQuery but it shouldn't take you long to translate:

    new Ajax.Request('process.php', {
        on401: function(response) {
            var redirect = response.getHeader('Location');
            document.location = redirect;
        }
    });
    

    In your PHP, output the following if the session is inactive:

    header('Location: http://example.com/login.php', true, 401);
    exit;
    

提交回复
热议问题