AngularJS 406 不接受 http post 请求



我在用于表单的 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, */*' 来尝试以下代码:

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);
 });

最新更新