[英]Login page AJAX redirect doesnt work
I use php with ajax. 我用ajax使用php。
<?php
/*
* Suppose, user has entered both correct login and password
* then server response is TO **REDIRECT** to profile page
*
/
header('location:/');
/*
*
* print <<<HTML
<scrpit> window . location = '/profile.php';</script>
HTML;
* also doesnt work
**/
?>
The problem is that it does redirect, but leaves part of authorization page, and the result = is mixed profile + auth page 问题是它确实重定向,但保留了部分授权页面,结果=混合配置文件+ auth页面
When using AJAX, redirecting using header()
will not work. 使用AJAX时,使用
header()
重定向将不起作用。
This is what will happen: 这将是会发生的事情:
What you need to do is: 你需要做的是:
$_SERVER['HTTP_X_REQUESTED_WITH']
has the value of xmlhttprequest
. $_SERVER['HTTP_X_REQUESTED_WITH']
是否具有xmlhttprequest
的值来确定请求是否为ajax。 Most JS libraries will send this. header('Location: /')
header('Location: /')
重定向header('Location: /')
echo('location.replace("\\")');
echo('location.replace("\\")');
If you are using AJAX call to the page as written above, using both header()
and JavaScript
to redirect won't work. 如果您正如上面所写使用AJAX调用页面,则使用
header()
和JavaScript
重定向将不起作用。 You need to use javascript in order to redirect page. 您需要使用javascript才能重定向页面。 Sample codes:
示例代码:
<?php
/**
* Suppose, user has entered both correct login and password
* then server response is TO **REDIRECT** to profile page
*/
// define login is false by default
$response = array();
response['Success'] = false;
// use if-condition to check if succeeded
// then assign it to true like this
response['Success'] = true;
die( json_encode($response) );
?>
// Javascript Code:
// You must decode it first since it's json
// Assuming that `theResponse` is the message callback
var responseStr = JSON.parse( theResponse );
if ( responseStr.Success ){
location.href = 'profile.php';
}else {
// show some message
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.