将数据发送到PHP文件

时间:2017-05-31 05:40:58

标签: php angularjs

我正在尝试将数据发送到php文件,但它无法正常工作。 这是我的代码:

App.js:

.controller('sign_up', function ($scope, $http) {
$scope.login = function () {

            var request = $http({
                method: "post",
                url: "js/login.php",
                data: {
                    email: $scope.email,
                    password: $scope.password
                },
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            });
            /* Successful HTTP post request or not */
            request.success(function (data) {
                if(data == '1'){
                    $scope.responseMessage = "Successfully Logged In";
                }
                else {
                    $scope.responseMessage = "Username or Password is incorrect";
                }
            });
    }
});

的index.html:

<div ng-controller='sign_up'>
  <input class="form-control" type="text" ng-model="email" name="email" 
   placeholder="Enter Your Email">
 <br>
  <input class="form-control" type="password" ng-model="password" 
     name="password" placeholder="Enter Your Password"><br>
   <button class="btn btn-success" ng-click="login()">Login</button><br>
    <span>{{responseMessage}}</span>
</div>

的login.php:

 <?php
   $postdata = file_get_contents("php://input");
   $request = json_decode($postdata);
   $email=$_POST['email']
   $password=$_POST['password']
    $email = $request->email;
    $password = $request->password;
    echo json_encode($email);
    echo "email=".$email;
    if($email == "two" && $password== "one"){

    echo "1";
    }
    else {
        echo "0";
    }

&GT;

1 个答案:

答案 0 :(得分:0)

使用$request = json_decode($postdata,TRUE);

<?php
   $postdata = file_get_contents("php://input");
   $request = json_decode($postdata,TRUE); 
   $email = $request->email;
   $password = $request->password;

   if($email == "two" && $password== "one"){

    echo "1";
   }
   else {
        echo "0";
   }
?>

还将内容类型更改为json

var request = $http({
                method: "post",
                url: "js/login.php",
                data: {
                    email: $scope.email,
                    password: $scope.password
                },
                headers: { 'Content-Type': 'application/json' }
});