Parse angularjs array in php


Parse angularjs array in php

我是Anggularjs的新手.我有一个表单并将ng模型数组发送到php文件,我收到错误未定义的索引,不知道如何在php中解析来自angularjs的发送数组。

网页表单的一部分

                   <div class="form-group" >
                        <label>Street<span class="error">*</span></label><span ng-show="!userForm.street.$pristine && userForm.street.$invalid || (userForm.street.$touched && !userForm.street)"  class="error">Must be a number</span>
                        <input type="text" id="strret"  class="form-control" name="userForm.street" ng-model="userForm.street" required ng-value="userForm.street" />
                    </div>
                    <div class="form-group" >
                        <label>Tel<span class="error">*</span></label><span ng-show="!userForm.tel.$pristine && userForm.tel.$invalid || (userForm.tel.$touched && !userForm.tel)"  class="error">Must be number</span>
                        <input type="text" class="form-control" name="userForm.tel" ng-model="userForm.tel" required   ng-pattern="/^[0]{1,13}$/" ng-value="userForm.tel"/>
                    </div>

控制器内部的 Angularjs 后置函数:

$http({
    method  :'POST',
    url:'post.php',
    data: $scope.userForm,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data){
alert(data);
    }).error(function(err) {
           alert(err);
    });

我的问题是如何在 PHP 中获取 userForm.street 和 userForm.tel $_POST[]...在stackoverflow上也有类似的问题,但是在php文件中没有任何如何做到这一点的例子。

为了以形式编码格式发送数据,您需要对其进行序列化。

内部默认设置是将其序列化为 JSON

使用$httpParamSerializerJQLike服务。

$http({
    method  :'POST',
    url:'post.php',
    data: $httpParamSerializerJQLike($scope.userForm),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success...

不要忘记在服务或控制器中注入将要使用的位置

否则,如果使用$http默认值以application/json发送数据,则需要使用file_get_contents('php://input')访问数据,因为'$_POST将为空

$data = json_decode(file_get_contents('php://input'));