Php 使用AJAX重定向

Php 使用AJAX重定向,php,jquery,html,ajax,Php,Jquery,Html,Ajax,JQUERY 登录表单 $('#myform').submit(function() { $.ajax( { type: "POST", url: 'checkit.php', data: { usernameuser: $("#usernameuser").val(), passworduser: $

JQUERY

登录表单

$('#myform').submit(function() 
{
        $.ajax(
        {
            type: "POST",
            url: 'checkit.php',
            data: 
            {
                usernameuser: $("#usernameuser").val(),
                passworduser: $("#passworduser").val()
            },
            success: function(html)
            {
                $(".gonewrong").html(""); 
                $(".gonewrong").show();
                $(".gonewrong").append(html);
            }
        });
    });
});
如果登录错误,一切正常,错误消息将显示在正确的位置,但是如果登录成功,它不会将我重定向到浏览器地址栏中的good.php,但我看到goneError中的页面(good.php)


我花了很多时间来解决这个问题。

您正在尝试重定向后端应用程序,在那里调用ajax。 你应该重定向首页,那里有javascript

PHP:(后端应用程序,无法在此处重定向)

脚本:(首页,在此处重定向)


您正在尝试重定向后端应用程序,在那里调用ajax。 你应该重定向首页,那里有javascript

PHP:(后端应用程序,无法在此处重定向)

脚本:(首页,在此处重定向)


为什么不使用JavaScript重定向

$('#myform').submit(function(e) 
{
    e.preventDefault();
    $.ajax(
    {
        type: "POST",
        url: 'checkit.php',
        data: 
        {
            usernameuser: $("#usernameuser").val(),
            passworduser: $("#passworduser").val()
        },
        success: function(status)
        {
           // check if successfully logged in:
           if('success'==status){
               //Success! user is logged in, need to redirect to good.php
               window.location.href='http://yoursite.com/good.php';
           }else{
               // Error! user is not logged in, show error message:
               $(".gonewrong").html(""); 
               $(".gonewrong").show();
               $(".gonewrong").append(status);
           }
        }
     });
  });
});

如果checkit.php打印了错误消息,则显示它;否则,重定向。

为什么不使用JavaScript重定向

$('#myform').submit(function(e) 
{
    e.preventDefault();
    $.ajax(
    {
        type: "POST",
        url: 'checkit.php',
        data: 
        {
            usernameuser: $("#usernameuser").val(),
            passworduser: $("#passworduser").val()
        },
        success: function(status)
        {
           // check if successfully logged in:
           if('success'==status){
               //Success! user is logged in, need to redirect to good.php
               window.location.href='http://yoursite.com/good.php';
           }else{
               // Error! user is not logged in, show error message:
               $(".gonewrong").html(""); 
               $(".gonewrong").show();
               $(".gonewrong").append(status);
           }
        }
     });
  });
});

如果checkit.php打印了错误消息,则显示它;否则,重定向。

您无法在AJAX响应中重定向,因为浏览器没有导航到页面。相反,检查JavaScript中的响应并从那里重定向。大概是这样的:

$('#myform').submit(function() 
{
        $.ajax(
        {
            type: "POST",
            url: 'checkit.php',
            data: 
            {
                usernameuser: $("#usernameuser").val(),
                passworduser: $("#passworduser").val()
            },
            success: function(response)
            {
                if (response.length > 0) {
                    $(".gonewrong").html(response); 
                    $(".gonewrong").show();
                }
                else {
                    location.href = 'good.php';  // redirect like this
                }
            }
        });
    });
});
在JavaScript中:

if ($numUsers>0) 
{
    session_start();
    $_SESSION['username'] = $username;
    echo 'success';
} 
else 
{
    echo 'Incorrect username/password';
}
这是未经测试的,您可能希望调试响应并确保它在字符串比较中实际匹配。您还可以更进一步,使用结构化JSON数据进行响应,这将使JavaScript代码不那么脆弱


但主要的一点是,服务器发起的重定向只在页面导航上起作用,而不在API请求(AJAX)中起作用。在使用AJAX的情况下,“应用程序”是客户端代码,它需要执行重定向。

您不能在AJAX响应中重定向,因为浏览器没有导航到页面。相反,检查JavaScript中的响应并从那里重定向。大概是这样的:

$('#myform').submit(function() 
{
        $.ajax(
        {
            type: "POST",
            url: 'checkit.php',
            data: 
            {
                usernameuser: $("#usernameuser").val(),
                passworduser: $("#passworduser").val()
            },
            success: function(response)
            {
                if (response.length > 0) {
                    $(".gonewrong").html(response); 
                    $(".gonewrong").show();
                }
                else {
                    location.href = 'good.php';  // redirect like this
                }
            }
        });
    });
});
在JavaScript中:

if ($numUsers>0) 
{
    session_start();
    $_SESSION['username'] = $username;
    echo 'success';
} 
else 
{
    echo 'Incorrect username/password';
}
这是未经测试的,您可能希望调试响应并确保它在字符串比较中实际匹配。您还可以更进一步,使用结构化JSON数据进行响应,这将使JavaScript代码不那么脆弱


但主要的一点是,服务器发起的重定向只在页面导航上起作用,而不在API请求(AJAX)中起作用。在使用AJAX的情况下,“应用程序”是客户端代码,它需要执行重定向。

对于您的解决方案,如果登录不成功,它会在checkit.php中回显“不正确的用户名/密码”,我希望它正确。它似乎跳过了else语句,不应该。我在写完答案4分钟后编辑了它。试过了吗?是的,我刷新了这个页面后,现在又试了一次,但奇怪的是,它不起作用。现在就试一下。我添加了e.preventDefault();这可能是问题所在吗?对于您的解决方案,如果登录不成功,它会在checkit.php中回显“不正确的用户名/密码”,我希望它正确无误。它似乎跳过了else语句,不应该。我在写完答案4分钟后编辑了它。试过了吗?是的,我刷新了这个页面后,现在又试了一次,但奇怪的是,它不起作用。现在就试一下。我添加了e.preventDefault();这就是问题所在吗?
success: function(html)
{
    if (html == 'success') {
        window.location = 'good.php';
    } else {
        $(".gonewrong").html(""); 
        $(".gonewrong").show();
        $(".gonewrong").append(html);
    }
}