0

Problem in sending data if i will append all parameters to form Data than some of will not updating.

$scope.updateHospital = function(){
            $scope.spList = []; 
            var selected = $("#lstSpe option:selected");
                var message = [];
                selected.each(function () {
                    message.push($(this).val());
                });

                message.forEach(function(entry){
                    $scope.spList.push($scope.id[entry]); 
                });

            var formdata = new FormData();
            console.log($scope.name+$scope.des+$scope.spList);//all three fields have correct value in it

            formdata.append("name",$scope.name);
            formdata.append("description",$scope.des);
            formdata.append("primarySpecialty",$scope.spList);



            $http({
            method : 'PUT',
            url    : 'url',               
            headers : {"authorization" : access_token,"Content-type":undefined},
            data   :   formdata
            })

if i append "spList" to my form data than "name" and "description" is not updating the value when i send only "name & description" it is working fine. from postman this is working fine with all three values.

Harsh sachdev
  • 217
  • 1
  • 2
  • 13
  • `primarySpecialty` should be `primarySpeciality` - too small an edit for me to make. Your method of gathering values does not seem like the 'angular' way. Have you considered making use of angulars two-way data binding rather than using jquery selectors? – IronAces Aug 11 '16 at 09:07
  • no that is not a prblm that is just a typo mistake i have field with name "primarySpecialty". primarySpecialty is saving its data but the prblm is when i append "primarySpecialty" than name and description field is not saving their value – Harsh sachdev Aug 11 '16 at 09:10
  • I don't see where `$scope.name` and `$scope.des` are assigned? – IronAces Aug 11 '16 at 09:12
  • i have two input box on html form with ng-model="name" & ng-model="des" before appending the data i check the console value. they are giving me right values – Harsh sachdev Aug 11 '16 at 09:15
  • i think their is prblm of serialization but i am not sure ! – Harsh sachdev Aug 11 '16 at 09:17
  • And when you inspect post data in console? What does the object look like that is being sent? – IronAces Aug 11 '16 at 09:19
  • I wouldn't recommend formdata for angular. You could bind your models to be `ng-model="formdata.name"` and `ng-model="formdata.des"` and then you could append primarySpecialty as `$scope.formdata.primarySpecialty = $scope.spList;` don't forgot to set `formdata` as `$scope.formdata = {};` – Sjoerd de Wit Aug 11 '16 at 09:37
  • Might be a serialization problem caused by adding the array. A solution for sending arrays on `FormData` can be found [here](http://stackoverflow.com/questions/14026539/can-i-append-an-array-to-formdata-in-javascript). But as previously stated you could take advantage of angular two way data bind instead of using `FormData`. – Stefan.B Aug 11 '16 at 09:56

0 Answers0