2017-04-01 97 views
1

我对用于表单的Post查询有问题。每次我尝试验证我的表格时,我收到了“406不能接受”的错误,并Object.data是空白..AngularJS 406不可接受http发布请求

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

};

II不明白为什么它不接受..

回答

3

你应该给一个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); 
}); 
+0

谢谢:)是工作 – DaxDev