Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs-将对象数组(JSON数据)发布到PHP页面_Php_Json_Angularjs - Fatal编程技术网

angularjs-将对象数组(JSON数据)发布到PHP页面

angularjs-将对象数组(JSON数据)发布到PHP页面,php,json,angularjs,Php,Json,Angularjs,我的JSON数据的示例如下: $scope.a = [{ "email": "keval@gmail", "permissions": { "upload": "1", "edit": "1" } }, { "email": "new@aa", "permissions": {

我的JSON数据的示例如下:

$scope.a = [{
            "email": "keval@gmail",
            "permissions": {
                "upload": "1",
                "edit": "1"
            }
        }, {
            "email": "new@aa",
            "permissions": {
                "upload": "1",
                "edit": "1"
            }
        }];
我想发布同样的内容,以下是我的方法:

$http({
    method: 'POST',
    url: 'backend/savePermissions.php',
    data: {
        mydata: $scope.a
    },
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
})
.success(function(data) {
    console.log(data);
});
PHP将接受请求并响应:

echo $_POST['mydata'];
我在调用之前尝试了
JSON.stringify
,并在回显时对其进行
JSON\u解码
;还是不行。我一直在尝试我能想到的所有可能性,以及我在网络上找到的东西/其他问题,但仍然不起作用。

我为您制作了plnkr

正如您所看到的,我发送了一个未格式化的原始JSON,然后使用php

<?php
  echo file_get_contents('php://input');

我使用它,通过它我可以发送数组JSON:

var array = {}
array['key1'] = value1;
array['key2'] = value2;

$http.post(URL, array)
  .success(function(data){
  })
  .error(function(){

});

尝试使用$httpParamSerializer或$httpParamSerializerJQLike

$http({
    method: 'POST',
    url: 'backend/savePermissions.php',
    data: $httpParamSerializer($scope.a),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
})
.success(function(data) {
    console.log(data);
});

我认为,如果数据对象中的属性值未定义,那么它甚至没有被发送。我尝试添加null,并且可以在控制台的请求头中看到它。对不起,我怀疑我是否听了你的话。不管怎样,我在下面找到了一个解决方案。谢谢。@keval您是否正在尝试使用AngularJS和PHP创建Web服务?如果是,那么我想知道您的json输出是否在jsonlint.com中解析json验证,因为我正在尝试做同样的事情(AngularJS+PHP中的webservice),并且我在jsonlint Parsing中获得完整的html是的,我的json根据jsonlint是有效的。我不理解你,你说的“获取整个html”是什么意思?你能在一个新问题中发布你的代码并给我链接吗?或者只要给我看一下代码,我就会试着看看出了什么问题。太好了!就是这一行,我得到了数据的回应;一切看起来都很好。非常感谢。
$http({
    method: 'POST',
    url: 'backend/savePermissions.php',
    data: $httpParamSerializer($scope.a),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
})
.success(function(data) {
    console.log(data);
});