まず、この質問が何度か出されていることを知っています。私は多くの投稿された解決策を試しましたが、何もうまくいきません..
これが尋ねられた他のいくつかの場所は次のとおりです。
- カスタム Angular $resource アクションのヘッダー パラメータを指定する方法
- リクエスト ペイロードではなく、フォーム データとしてデータを投稿するにはどうすればよいですか?
- AngularJS でアプリケーション全体の HTTP ヘッダーを設定する
- https://groups.google.com/forum/#!msg/angular/Mtsf-YdwwWo/P_Ui4t_DiXkJ
試み:
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' を使用します!?