http帖子返回奇怪的JSON

时间:2016-05-09 16:04:56

标签: php angularjs xmlhttprequest

我正在开发我的第一个API /后台(所以我是MAMP和PHP的新手),这是我的问题: 我在我的脚本上使用$ http和post方法(我正在使用AngularJS)并且我将一个对象作为数据发送。我的PHP应该返回我发送的内容但是我得到了这个东西:

Array([{"id":1}] => )

我尝试了很多东西,所以我不知道该怎么做。 这是我的代码

JS:

    $http({
    method:"POST",
    url:"api/getInfos.php",
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data:{id:1}
})
.then(function successCallback(response) {
    console.log(response.data);
    if(response.data!="null")
        $scope.presentation=response.data;
    else{
        $scope.presentation={title:"Error",content:"No content"};
    }
}, function errorCallback(response) {
    console.log("errorCallback");
});

PHP:

header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
header('Access-Control-Allow-Methods: GET, POST, PUT');
require "openConnection.php";

print_r($_POST);

谢谢!

编辑: 我使用$post=json_decode(file_get_contents('php://input'),true)解决了我的问题。但是,如果你能找到解决方案并向我解释,欢迎你。

0 个答案:

没有答案
相关问题