【问题标题】:AngularJS 406 not acceptable http post requestAngularJS 406 不可接受的 http post 请求
【发布时间】:2017-04-01 16:40:47
【问题描述】:

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

};

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

【问题讨论】:

    标签: angularjs json http http-post http-status-code-406


    【解决方案1】:

    你应该发送一个 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);
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2014-01-27
      • 2019-12-15
      • 2017-12-24
      • 2014-12-24
      相关资源
      最近更新 更多