Search code examples
angularjsjsonhttphttp-posthttp-status-code-406

AngularJS 406 not acceptable http post request


I have a problem with a Post query that I use for a form. I get a "406 not acceptable" error every time I try to validate my form, and Object.data is blank..

var edit = function(form){
var token = window.localStorage.getItem('token');
$ionicLoading.show();
return $http({
       method  : 'POST',
       url     : API.url + '/user',
       headers : {Authorization : 'Bearer ' + token},
       transformRequest: function(data, headers){
            console.log(headers);
            headers = angular.extend({}, headers, {'Content-Type': 'application/json;charset=UTF-8'});
            console.log(headers);
            console.log(data);
            console.log(angular.toJson(data));
            return angular.toJson(data); // this will go in the body request
            },
           data    : form
   }).then(function(result) {
    console.dir(result.data);
},function errorCallback(response) {
      console.log(response);
 });

};

I I do not understand why it does not accept..


Solution

  • You should to send a json data to your server try this following code by adding 'Accept': 'application/json, */*' to your header:

    var edit = function(form){
    var token = window.localStorage.getItem('token');
    $ionicLoading.show();
    return $http({
           method  : 'POST',
           url     : API.url + '/user',
           headers : {
              Authorization : 'Bearer ' + token,
             'Accept': 'application/json, */*'
           },
           transformRequest: function(data, headers){
                console.log(headers);
                headers = angular.extend({}, headers, {'Content-Type': 'application/json;charset=UTF-8'});
                console.log(headers);
                console.log(data);
                console.log(angular.toJson(data));
                return angular.toJson(data); // this will go in the body request
                },
               data    : form
       }).then(function(result) {
        console.dir(result.data);
    },function errorCallback(response) {
          console.log(response);
     });