AngularJS 406不接受http post请求

Dax*_*Dev 2 json http http-post http-status-code-406 angularjs

我对用于表单的Post查询有问题.每次我尝试验证表单时都会出现"406不可接受"错误,而Object.data为空白.

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);
 });
Run Code Online (Sandbox Code Playgroud)

};

我不明白为什么不接受..

小智 6

您应该将json数据发送到您的服务器,通过添加'Accept': 'application/json, */*'到您的标头尝试以下代码:

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);
 });
Run Code Online (Sandbox Code Playgroud)