2015-11-04 70 views
-1

我正在使用Angular Js的登录表单.HTML和验证部分工作正常。Http发送不发送数据到角js的另一个页面

问题是当我使用HTTP POST方法将数据发送到我的servicecall PHP页面并希望将其保存到数据库。

我已经

headers: {'Content-Type': 'application/x-www-form-urlencoded'} 

我已经通过设置形式的加密类型,以

enctype="application/x-www-form-urlencoded" 

为well.But他们没有试图在JS设置这个尝试了所有的办法是工作我不能够通过$ _REQUEST,$ _POST,$ _GET获得任何这些方法的数据。

我也试过使用PHP库函数。

$postdata = file_get_contents("php://input"); 

但它给我无法处理,因为POST数据的数量可能是数百一些奇怪的字符串。

所以这里还有其他解决问题的方法。

代码为http请求

var req = { 
    method: 'POST', 
    url: 'servicecall.php', 
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
    data: { 
      username: $scope.loginData.username, 
      password: $scope.loginData.password, 
      action : 'checkLogin' 
     } //First way of sending data 

//Second way of sending data which is also not working 
//data: "{username:"+$scope.loginData.username+",password:"+$scope.loginData.password+",action:checkLogin}" 

} 

$http(req).then(function(response){ 
     console.log(response); 
    }, function(response){ 
     alert("Error "+response); 
    }); 

在PHP页面我做

print_r($_REQUEST); 
print_r($_POST); 

但它打印只是空白阵列状array{}

+2

我们可以看到完整的'http http({})'对象。我怀疑在将数据发送到服务器之前如何格式化数据存在问题。 – Ohgodwhy

+1

请提供$ http调用的代码。似乎有一个问题。 –

+0

你可以添加代码snipet –

回答

1

下面,我使用了相同的目的代码。希望这可以帮助你。

var app = angular.module('angularApp', []); 

app.controller('loginCtrl', function ($scope, $http) { 

    $scope.login = function() { 

      var request = $http({ 
       method: "post", 
       url: "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"; 
       } 
      }); 
    } 
}); 
+0

没有改变产生相同的产量。 –