简体   繁体   English

为什么在使用window.location和window.location.href时无法使用javascript重定向Page

[英]Why Page is not redirected using javascript on using window.location and window.location.href

I am using a login system for my website. 我正在为我的网站使用登录系统。 What I am doing is that when the user clicks the submit button an ajax request is being executed to target php script which logs in the user and if user is logged in successfully then echo's back the message "login_successful" to the ajax request. 我做的是,当用户点击提交按钮正在执行一个Ajax请求针对PHP脚本登录的用户时,如果用户登录成功,然后echo's回消息“login_successful” Ajax请求。 By using if statement I check whether the message is "login_successful" else display the error. 通过使用if语句,我检查消息是否为“ login_successful”, else显示错误。

If the message is "login_successful" the script redirect the user to the new page 'user.php' by using window.location = 'user.php'; //I have also tried this. window.location.href = 'user.php'; 如果消息是“ login_successful”,则脚本通过使用window.location = 'user.php'; //I have also tried this. window.location.href = 'user.php';将用户重定向到新页面window.location = 'user.php'; //I have also tried this. window.location.href = 'user.php'; window.location = 'user.php'; //I have also tried this. window.location.href = 'user.php';

But it doesn't work It simply stays on the login page and nothing happens. 但这不起作用,它只是停留在登录页面上,什么也没有发生。 but when i check the page source after logging in but no redirection takes place then i got a surprise that the source of the page is for user.php instead of login.php. 但是当我登录后检查页面源但没有发生重定向时,我感到惊讶的是页面源是user.php而不是login.php。 Somehow window.location hasn't redirected the page but bring the source of user.php to login.php. 不知何故, window.location并未重定向页面,而是将user.php的源代码带到了login.php中。 It's all messed up and I couldn't solve the problem. 都搞砸了,我解决不了问题。

Here is the login function which performs the ajax request and then redirect the user- 这是执行ajax请求然后重定向用户的登录功能-

function login()
{
    var e = _("email").value;// Grab the email input by the user.
    var p = _("password").value;// Grab the password input by the user.
    if(e === "" || p === "")// Check if they are empty.
    {
        _("status").innerHTML = "Fill out all the form data";//Display error message if fields are empty.
    }
    else
    {
        _("loginbtn").style.display = "none";// Hide the login button
        _("status").innerHTML = 'please wait ...';//Tell the user to wait while the script works.
        // Below id another function which is defined in other js file.
        var ajax = ajaxObj("POST", "login.php"); //start the ajax request.
        ajax.onreadystatechange = function()// Wait for the request to be complete
        {
            if(ajaxReturn(ajax) === true) // Ensures if the response is recieved
            {
                var response = ajax.responseText;// Put the response in a variable
                if(response === 'login_successful')// Check if user is logged in succesfully.
                {
                    window.location = 'user.php';//Redirect the user to the new page.(Not working)
                }
                else
                {
                    _('status').innerHTML = response;// Display the response
                    _("loginbtn").style.display = "block";// Display the login button
                }
            }
        };
        ajax.send("e="+e+"&p="+p);
    }
}

I am using the latest version of chrome on a 64 bit windows machine. 我在64位Windows计算机上使用的是最新版的chrome。

Update 更新资料

OK guys I have verified that the response via php script is 'login_successful' and it doesn't contains any line so that we can trim on it. 好的,我已经确认通过php脚本的响应为'login_successful',并且其中不包含任何行,因此我们可以对其进行裁剪。

check the result by alert in between response condition & use 在响应条件和使用之间通过警报检查结果

window.location.href='correctfilepath.php';

correct path of file to be redirected. 重定向文件的正确路径。 Check your user.php also as it will not print anything if it will have some error in code 还要检查您的user.php,因为如果代码有错误,它将不会打印任何内容

if you use location i prefer to use a absolute path. 如果您使用位置,我更喜欢使用绝对路径。 Maybe this is your problem and a redirect will be working with this. 也许这是您的问题,重定向将对此起作用。 For example: 例如:

location.href = '/user.php';

If you want to redirect to a url do it like this: 如果要重定向到URL,请执行以下操作:

location.href = '//www.google.com';

(it will automatically use http or https based on the current scheme. (它将根据当前方案自动使用http或https。

Try setting the full URL for redirection 尝试设置完整的URL以进行重定向

window.location = window.location.protocol + "//" + window.location.host + "/user.php" window.location = window.location.protocol +“ //” + window.location.host +“ /user.php”

instead of window.location = 'user.php'; 而不是window.location ='user.php';

Edited: 编辑:

window.location = "//" + window.location.host + "/user.php" window.location =“ //” + window.location.host +“ /user.php”

Javascript redirection works without the protocol (http or https) also. Javascript重定向也可以在没有协议(http或https)的情况下使用。

if(response === 'login_successful')// Check if user is logged in succesfully.
                {
                    window.location = 'user.php';//Redirect the user to the new page.(Not working)
                }
                else
                {
                    _('status').innerHTML = response;// Display the response
                    _("loginbtn").style.display = "block";// Display the login button
                }

In the else block you had given _('status').innerHTML = response; 在else块中,您给出了_('status')。innerHTML = response; check that block is executing or not if its is executing then try to change the (=== as == )in the if condition hope it may work. 检查该块是否正在执行,如果它正在执行,则尝试在if条件希望它可以工作的情况下将(=== as ==)更改。

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

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