Deepak Patidar
Deepak Patidar

Reputation: 394

Angular Http put method giving error while passing data (server expecting form param )

I am trying to use the http put method to pass some data on server and server is looking for form param in put method.

var deferred = $q.defer();
$http({
  method: 'PUT',
  url:  'http.hello.com/rest/sample/'+fileName,
  data:  {status: status}
}).success(function(data) { 
  deferred.resolve(data);
}).error(function() {
  deferred.reject(data);
});
return deferred.promise;

but server still giving missing data custom error then how can I pass the data in put method. also try $.param() but it convert the data into string that's not correct

Upvotes: 1

Views: 211

Answers (1)

Yerken
Yerken

Reputation: 1942

  1. First of all $http itself returns a promise, so you do not need to $q
  2. To pass the data in a neater way you can use the following

'use strict';

(function() {

    function AppService($http) {

        var AppService = {
            putStatus(status, filename) {
                return $http.put('http.hello.com/rest/sample/'+filename, {status: status})
            }
        };
        return AppService;
    }

    angular.module('app')
        .factory('AppService', AppService);

})();

Use as follows (somewhere in the controller)

AppService.putStatus(status, filename)
.then((response) => {
// do something on success
})
.catch((err) => {
//if error occured
});

Upvotes: 1

Related Questions