使用PHP检索AngularJS$http.post服务器数据


Retrieve AngularJS $http.post server data using PHP

我正在调用$http.post函数,但无法获取另一端的数据,调用成功,但无法在我的PHP脚本中检索数据

AngularJS

    var data = {
        firstname: $scope.first_name,
        lastname: $scope.last_name,
        email : 'test@fff.com',
        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;

也尝试过这个,也不起作用

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

在$http中为每个帖子传递数据。例如

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

这里是我为服务器端脚本准备的数据,该脚本由范围中的Formdata值组成。

如果您想将json作为JSON发送,请使用JSON.stringify()包装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;'}
})