【问题标题】:Trouble changing Content-Type for POST requests with ngResource (1.0.6 and 1.1.4)使用 ngResource(1.0.6 和 1.1.4)更改 POST 请求的 Content-Type 时遇到问题
【发布时间】:2013-05-03 00:48:34
【问题描述】:

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

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

尝试:

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

【问题讨论】:

  • 我记得读过您必须在帖子中包含内容才能接受您的标题更改。只是一个疯狂的猜测......
  • 就是迈克! $scope.user = SystemConnect.post({}); 工作,非常奇怪。把它放在一个答案中。
  • 最简单的答案,很高兴为您提供帮助
  • 上述哪些尝试 (1-4) 确实与 .post({}) 结合使用?

标签: javascript angularjs angular-resource


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2012-07-07
    • 2020-04-18
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多