2016-04-06 28 views
0

我打電話給$ http.post函數,但無法獲取另一端的數據,調用成功,但無法檢索我的PHP腳本中的數據

AngularJS

var data = { 
     firstname: $scope.first_name, 
     lastname: $scope.last_name, 
     email : '[email protected]', 
     user : userID 
    }; 
    var config = { 
     headers : { 
      'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' 
     } 
    } 
$http.post('http://xxx-env.us-east-1.elasticbeanstalk.com/apipost/updateusersetting/user/'+userID, data, config).success(function (data, status, headers, config) { 
     alert('yay'); // this works and gets called 
    }) 
    .error(function (data, status, header, config) { 

}); 

服務器端PHP:

$data = json_decode(file_get_contents("php://input")); 
    $email = $data->email; 
    $user = $data->user; 
    $firstname = $data->firstname; 
    $lastname = $data->lastname; 

也嘗試在$ HTTP每個崗位這一點,不工作要麼

 $email = $_POST['email']; 
     $user = $_POST['user'];//$data->user; 
     $firstname = $_POST['firstname']; //$data->firstname; 
     $lastname = $_POST['lastname']; ;//$data->lastname; 
+0

請問您可以發佈$ _POST的var_dump嗎? – atefth

回答

0

傳遞數據。例如

$http({ 
      method :'POST', 
      url:'server/company/addData', 
      data: $scope.FormData, 
      headers: {'X-API-KEY': $rootScope.currentUser.key} 

     }) 
      .success(function(data){ 
       console.log(data); 

      }); 

這裏的數據是我在服務器端腳本中發佈的,它包含範圍中的Formdata值。

0

使用JSON.stringify()來包裝你的json,如果你想發送它作爲JSON

var parameter = JSON.stringify(data); 

以下是您如何撥打電話的方法。

$http({ 
    method: 'POST', 
    url: 'http://xxx-env.us-east-1.elasticbeanstalk.com 
       /apipost/updateusersetting/user/'+userID', 
    data: parameter , 
    headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'} 
}) 
相關問題