2016-06-29 193 views
0

我有以下代码:AngularJS不发送HTTP POST请求

application.controller('userLoginController', function($scope,$http){ 
window.document.title = "User Login"; 
$scope.form = { username: '',password: ''}; 
$scope.submitLogin = function(){ 
    var config = { 
     method: 'POST', 
     url: 'server_app/login.php', 
     data: { 
      'username' : $scope.form.password, 
      'password' : $scope.form.password 

     } 
    }; 
    var request = $http(config); 
    request.then(function (response){ 
     $scope.errorMessage = response.data; 
    },function(error){ 
     $scope.errorMessage = error.data; 
     }) 
    } 
}); 

我试图发送POST请求到后端服务器,看起来像:

var_dump($_POST); 

与提交我的数据后按钮我应该用$ _POST返回数组。那 Insteed我得到

array (size=0) 
    empty 

我的HTML代码看起来像:

<input placeholder="Login" class="searchProduct" autocomplete="off" name="username" type="text" ng-model="form.username"><br> 
<input placeholder="Password" class="searchProduct" autocomplete="off" type="password" name="password" ng-model="form.password"/> 
<div class="button" ng-click="submitLogin();">Login</div> 

我看不出这里有什么问题..

回答

0

我希望你在php文件中接收的数据如下,因为角码看起来很好。

$ params = json_decode(file_get_contents('php:// input'),true);

+0

确实谢谢你,但我没有得到一件事,为什么我需要这样写呢?例如使用Jquery,我只是在做var_dump($ _ POST)? –

+0

当你用formet enctype =“multipart/form-data”发送表单数据时,你可以用$ _POST接收它,因为你发送数据为json,你需要使用file_get_contents('php:// input')。 –

+0

好的检查!谢谢! –

0

您可以使用下面的代码

$http.get('/someUrl', config).then(successCallback, errorCallback); 
$http.post('/someUrl', data, config).then(successCallback, errorCallback); 
+0

谢谢,但它仍然没有回答我的问题,我想知道它为什么不起作用。 –