Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set header to the $http.post method AngularJS 1.3.5?

I wrote a rest webservice and to avoid the error I got in my java code which is: The registered message body readers compatible with the MIME media type, I have to add to my $http.post a 'Content-Type': 'application/x-www-form-urlencoded' or 'Content-Type': 'application/json'. I am using Angularjs 1.3.5 and whenever I tried to add the headers {content-type.... }, I failed. What can I do exactly to solve my problem?

$scope.save = function(url,data) {
    var data_stack = $scope.stack;
		$http.post(url, data_stack)
            .then(
           function(response){
                }, 
           function(response){
                });
}
<form ng-submit="save()">
      <input ng-model="stack"></input>
      <button type="submit">Save</button>
</form>
like image 863
nour nour Avatar asked Dec 08 '25 14:12

nour nour


1 Answers

var req = {
 method: 'POST',
 url: 'http://example.com',
 headers: {
   'Content-Type':'application/x-www-form-urlencoded' 
   // or  'Content-Type':'application/json'
 },
 data: { test: 'test' }
}

$http(req).then(function(){...}, function(){...});
like image 154
radek. Avatar answered Dec 10 '25 04:12

radek.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!