AJAX请求通过angular js POST到一个PHP脚本返回整个html页面而不重定向


AJAX request through angular js POST to a PHP script returning whole html page not redirecting

JS

var app = angular.module('br', []);
app.controller('LoginCtrl', function($scope, $http) {
    $scope.valid = function() {
        var request = $http({
            method: "post",
            url: "php_files/login.php",
            data: {
                em: $scope.em,
                pw: $scope.pw
            },
        });
        request.success(function(data) {
            if (data == "invalid") {
                $scope.message = "Invalid email id or password!"
            }                
        });
    }
});

HTML

<body ng-controller="LoginCtrl">
    <form method="post">
        <table>
            <tr>
                <td>Email</td>
                <td><input type="email"  required="required" ng-model="em"/></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" required="required" ng-model="pw" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Login" ng-click="valid()"/></td>
            </tr>
            <tr>
                <td colspan="2" align='center' style="color: red">{{message}}</td>
            </tr>
        </table>
    </form>
</body>

PHP脚本

include './include_db.php';
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$em = $request->em;
$pw = $request->pw;
$query = "select em,pw from users where em='$em' and pw='$pw'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$got_em = $row["em"];
$got_pw = $row["pw"];
if ($em != $got_em && $pw != $got_pw) {
    echo "invalid";
} else {
    session_start();
    $_SESSION["em"] = $em;
    header("Location:home.html");
}

现在我想要的是,如果电子邮件id和密码错误,它应该返回无效,这是有效的,但如果它们是正确的,它应该重定向到不起作用的主页。

在php_files/login.php中:

if(!valid($username,$password)){
die("invalid");
} else {
die("https://logged_in_address");
}

在您的javascript 中

                            request.success(function(data) {
                                if (data == "invalid") {
                                    $scope.message = "Invalid email id or password!"
                                } else {
                              window.location.replace(data);
/*redirect to the logged in page provided by login.php */
                               }
                            });