2016-06-21 2 views
1

각도 배열에서 API 호출 나머지 데이터 배열을 게시하려면 여기 내 샘플 코드입니다. 당신은 제각각 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) {}); 
}; 

내 서비스 :

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> 
+0

가능한 [Angular $ http POST에서 데이터 배열을 전달하십시오] (0120-337-333)/ –

+0

저기서 해결책이 있지만 angular.js와 같은 오류가 발생합니다 : 13708 TypeError : 정의되지 않은 'name'속성을 읽을 수 없습니다. at jquery.js : 8236 Function.jQuery.extend.each (jquery.js : 359) $ scope.addRowToGrid (StudentController.js : 42) at fn ( (angular.)에있는 Function.jQuery.param (jquery.js : 8235) 범위의 Object.create (ContactService.js : 16) 에 있습니다. js : 14605), : 4 : 227) at expensiveCheckFn (angular.js : 15694) 콜백시 (angular.js : 25622) 범위에서 . $ eval (angular.js : 17444) 범위에서 $ 적용 @MikeCheel – vyshnavi

+0

질문과 답변을 지우지 마십시오. 그것의 사이트에 대한 규칙은 – naveen

답변

0

귀하의 코드가 싶지는 서버에 배열을 보내 해달라고 제안 urlencoded form-data을 보내고 싶습니다.

서비스 :

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; 
    service.CreateUser(jsonData).then(function(response){ 
     //success callback 
     var data = response.data; 
     //do something with the data 
    },function(response){ 
     //error callback 
    }); 
}; 

당신이 기본적인 아이디어를 얻을 바랍니다.

P.S : .success을 사용하지 마십시오. 그것은 쓸모가 없습니다.

+0

고맙습니다 @naveen – vyshnavi

관련 문제