AngularJS 406不接受http post请求

时间:2017-04-01 16:40:47

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

我对用于表单的Post查询有问题。我得到了#34; 406不能接受"每当我尝试验证表单时出错,并且Object.data为空。

<ContentPage.Resources>
<ResourceDictionary>
    <Thickness x:Key="padding">45,0</Thickness>
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout Padding="{StaticResource padding}">
    <Label Text="{StaticResource padding}"/>
</StackLayout>

};

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

1 个答案:

答案 0 :(得分: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);
 });