1

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

这是被问到的其他几个地方:

尝试:

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!?

4

1 回答 1

3

我记得读过您必须在帖子中包含内容才能接受您的标题更改。

$scope.user = SystemConnect.post({});
于 2013-05-08T18:23:27.873 回答