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 は POST に 'Content-Type: text/plain' を使用します!?

4

1 に答える 1

3

ヘッダーの変更を受け入れるには、投稿にコンテンツを含める必要があることを読んだことを覚えています。

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