AngularJS 406 不可接受的 http post 请求

AngularJS 406 not acceptable http post request

我对用于表单的 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);
 });

};

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

您应该向您的服务器发送 json 数据 通过将 'Accept': 'application/json, */*' 添加到您的 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);
 });