使用ngResource(1.0.6和1.1.4)更改POST请求的Content-Type时遇到问题

时间:2013-05-08 17:20:23

标签: javascript angularjs angular-resource

首先,我知道这个问题已被问过好几次了。我尝试了许多已发布的解决方案,没有任何对我有用..

以下是其他一些地方:

尝试:

var app = angular.module('theApp', ['app.services']);


app
  .config(['$httpProvider', function ($httpProvider) {
    // Try (1): This doesn't work
    $httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';
    // Try (2): This doesn't work either
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
  }])


angular.module('app.services', ['ngResource'])
  // Resource for Drupal system/connect.post API (via services.module)
  .factory('SystemConnect', function($resource, $http) {
    // Try (3): There's no way this should work. But what the hell let's try!
    $http.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';
    $http.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';

    return $resource('api/system/connect.json', {}, {
      post: {
        method: 'POST',
        params: { },
        isArray: true,
        // Try (4): This doesn't work either
        headers: { 'Content-Type': 'application/json;charset=utf-8' }
      }
    });
  });


function SomeCtrl($scope, SystemConnect) {
  // FAIL, this results in "406 Not Acceptable: Unsupported content type application/xml"
  $scope.user = SystemConnect.post();
}
app.controller('SomeCtrl', SomeCtrl);

听起来很多人以前都解决了这个问题。有人可以让我知道正确的方法吗?

PS:奇怪的是,在Firefox中运行此代码时,Angular使用'Content-Type:text / plain'作为POST!?

1 个答案:

答案 0 :(得分:3)

我记得读过你必须在帖子中加入内容才能接受你的标题更改。

$scope.user = SystemConnect.post({});