Angular-js post总是得到错误(0)作为响应

时间:2015-11-22 05:42:57

标签: javascript php jquery angularjs

感谢您提前

嗨尝试了angular-js http post功能,但总是得到0作为错误响应,我尝试了很多,但它没有帮助,所以请给我一个解决问题的想法。

我使用了以下文件

var app = angular.module('angularPostPHP', []);
app.controller('regCtrl', function ($scope, $http) {
$scope.login = function () {
var request = $http({
    method: 'post',
    url:'http://localhost/HAP_testing/Registration.php',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
    data: {
        email: $scope.UserName,
        pass: $scope.Pwd
    }
});
/* Check whether the HTTP Request is successful or not. */
request.success(function (data) {
   // document.getElementById("message").textContent = "You have login successfully with email "+data;
    alert("done"+data);
});

request.error(function(status)
{
 alert("Error"+status);
});

}

});

<?php  
   	// check username or password from database
    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $email = $request->UserName;
    $password = $request->pwd;
    if($email == "dhaya" && $password== "dd"){
    	echo "1";
    }
    else {
    	echo "dhaya";
    }
 
?>

2 个答案:

答案 0 :(得分:0)

我认为问题在于:

$email = $request->UserName;
$password = $request->pwd;

您传递了这些数据:

data: {
    email: $scope.UserName,
    pass: $scope.Pwd
}

所以我认为那必须是:

$email = $request->email;
$password = $request->pass;

这样做的一种方法可能是:     

// check username or password from database

$email = $_POST['email'];
$password = $_POST['pass'];

if($email == "dhaya" && $password== "dd"){
    echo "1";
}
else {
    echo "dhaya";
}

<强>编辑: 改变了密码以传递

修改

我已经确认这适用于以下卷曲请求:

curl -d "email=dhaya&pass=dd" "chase.phpdev.gurutech.ws/question_test.php"

返回“1”。我没有设置并测试角度。但我发现至少有一个问题。

答案 1 :(得分:0)

感谢大家,最后我得到了解决。它因请求数据而发生。

var app = angular.module('angularPostPHP', []);
app.controller('loginCtrl', function ($scope, $http) {

$scope.login = function () {
var request = $http({
    method: 'POST',
    url:'../HAP_testing/Loginphp.php',
    // headers: {'Authorization': 'Token token=xxxxYYYYZzzz'}
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;'},
    data:  $.param({
            email: $scope.username,
            pass: $scope.password
        }),
});
/* Check whether the HTTP Request is successful or not. */
request.success(function (data) {
    alert("done");
});

request.error(function(status)
{
 alert("Error"+status.code);
});