Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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 如何发布数据数组_Angularjs - Fatal编程技术网

Angularjs 如何发布数据数组

Angularjs 如何发布数据数组,angularjs,Angularjs,我想在angular js中发布数据数组到rest api调用,这是我的示例代码。我是angular js的新手,您能给我一些建议吗 这是我的控制器: $scope.addRowToGrid = function() { var jsonData = $scope.create; console.log("create", $scope.create); var sendDataForCreate = [jsonData.PIDM, jsonData.ph

我想在angular js中发布数据数组到rest api调用,这是我的示例代码。我是angular js的新手,您能给我一些建议吗

这是我的控制器:

$scope.addRowToGrid = function() {
    var jsonData = $scope.create;
    console.log("create", $scope.create);
    var sendDataForCreate = [jsonData.PIDM,
        jsonData.phoneCode, jsonData.phoneNumber, jsonData.sequenceNumber, jsonData.comment,
        jsonData.areaCode, jsonData.dataOrigin, jsonData.userId
    ];
    service.create(sendDataForCreate).success(
        function(response) {});
};
$scope.addRowToGrid = function () {
    var jsonData = $scope.create;
    service.CreateUser(jsonData).then(function(response){
        //success callback
        var data = response.data;
        //do something with the data
    },function(response){
        //error callback
    });
};
这是我的服务:

angular.module('ContactApp').factory(
    'service', [
        '$http', 'Settings',
        function($http, Settings) {
            return {
                create: function(jsonData) {
                    return $http.post('http://localhost:20080/api/person/phone/v1/jonesl?token=w9cJTKsjhumFoFXzQ5fzw9XQBc', {
                            "operation": "create",
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        }).success(
                            function(response) {
                                console.log("create", response);
                                return response.data;
                            });
                }
            }
        }
    ]);
html:

<form name="userForm" ng-submit="addRowToGrid()">
    <label>phone number</label>
    <input type="text" name="pidm" 
           class="form-control" 
           ng-model="create.pidm">
    <label>phone number</label>
    <input type="text" name="areaCode" 
           class="form-control" 
           ng-model="create.areaCode">
    <label>phone number</label>
    <input type="text" name="phoneNumber" 
           class="form-control" 
           ng-model="create.phoneNumber">
    <label>phone number</label>
    <input type="text" name="sequenceNumber" 
           class="form-control" 
           ng-model="create.sequenceNumber">
    <label>phone number</label>
    <input type="text" name="phoneCode" 
           class="form-control" 
           ng-model="create.phoneCode">
    <label>phone number</label>
    <input type="text" name="comment" 
           class="form-control" 
           ng-model="create.comment">
    <label>phone number</label>
    <input type="text" name="dataOrigin" 
           class="form-control" 
           ng-model="create.dataOrigin">
    <label>phone number</label>
    <input type="text" name="userId" 
           class="form-control" 
           ng-model="create.userId">
    <button type="submit" class="btn btn-primary">
        Submit
    </button>
</form>

电话号码
电话号码
电话号码
电话号码
电话号码
电话号码
电话号码
电话号码
提交

您的代码表明您不想向服务器发送数组,而是想发送
urlencoded表单数据

服务:

angular.module('ContactApp').factory('service', [
        '$http', '$httpParamSerializerJQLike', 'Settings',
        function ($http, $httpParamSerializerJQLike, Settings) {
            var createUserUrl = 
                "http://localhost:20080/api/person/phone/v1/jonesl?token=blah";
            var CreateUser = function (DTO) {
                //with jQuery dependency
                //var dataToSend = $.param(DTO);
                //without jQuery dependency
                var dataToSend = $httpParamSerializerJQLike(DTO);
                var config = {
                    method: "POST",
                    url: createUserUrl,
                    data: dataToSend,
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                };
                return $http(config);
            };
            return {
                CreateUser: CreateUser
            }
        }
]);
控制器:

$scope.addRowToGrid = function() {
    var jsonData = $scope.create;
    console.log("create", $scope.create);
    var sendDataForCreate = [jsonData.PIDM,
        jsonData.phoneCode, jsonData.phoneNumber, jsonData.sequenceNumber, jsonData.comment,
        jsonData.areaCode, jsonData.dataOrigin, jsonData.userId
    ];
    service.create(sendDataForCreate).success(
        function(response) {});
};
$scope.addRowToGrid = function () {
    var jsonData = $scope.create;
    service.CreateUser(jsonData).then(function(response){
        //success callback
        var data = response.data;
        //do something with the data
    },function(response){
        //error callback
    });
};
希望你了解基本的想法


注意:不要使用
.success
。它已经过时。

您的代码表明您不想向服务器发送数组,而是想发送
urlcoded表单数据

服务:

angular.module('ContactApp').factory('service', [
        '$http', '$httpParamSerializerJQLike', 'Settings',
        function ($http, $httpParamSerializerJQLike, Settings) {
            var createUserUrl = 
                "http://localhost:20080/api/person/phone/v1/jonesl?token=blah";
            var CreateUser = function (DTO) {
                //with jQuery dependency
                //var dataToSend = $.param(DTO);
                //without jQuery dependency
                var dataToSend = $httpParamSerializerJQLike(DTO);
                var config = {
                    method: "POST",
                    url: createUserUrl,
                    data: dataToSend,
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                };
                return $http(config);
            };
            return {
                CreateUser: CreateUser
            }
        }
]);
控制器:

$scope.addRowToGrid = function() {
    var jsonData = $scope.create;
    console.log("create", $scope.create);
    var sendDataForCreate = [jsonData.PIDM,
        jsonData.phoneCode, jsonData.phoneNumber, jsonData.sequenceNumber, jsonData.comment,
        jsonData.areaCode, jsonData.dataOrigin, jsonData.userId
    ];
    service.create(sendDataForCreate).success(
        function(response) {});
};
$scope.addRowToGrid = function () {
    var jsonData = $scope.create;
    service.CreateUser(jsonData).then(function(response){
        //success callback
        var data = response.data;
        //do something with the data
    },function(response){
        //error callback
    });
};
希望你了解基本的想法


注意:不要使用
.success
。它已经过时。

可能重复我使用了那边的解决方案,但是我得到了一些错误,比如angular.js:13708 TypeError:undefined在Function.jquery.extend.each(jquery.js:359)在Function.jquery.param(jquery.js:8235)在Object.create(ContactService.js:16)无法读取未定义的属性“name”在Scope.$Scope.addRowToGrid(StudentController.js:42)在fn(eval at(angular.js:14605),:4:227)在expensiveCheckFn(angular.js:15694)在callback(angular.js:25622)在Scope.$eval(angular.js:17444)在Scope.$apply@mikecheelp请不要删除您的问题及其答案。它违反了站点规则可能重复我使用了那边的解决方案,但是我得到了一些错误,比如angular.js:13708 TypeError:undefined在Function.jquery.extend.each(jquery.js:359)在Function.jquery.param(jquery.js:8235)在Object.create(ContactService.js:16)无法读取未定义的属性“name”在Scope.$Scope.addRowToGrid(StudentController.js:42)在fn(eval at(angular.js:14605),:4:227)在expensiveCheckFn(angular.js:15694)在callback(angular.js:25622)在Scope.$eval(angular.js:17444)在Scope.$apply@mikecheelp请不要删除您的问题及其答案。这违反了网站规则