AngularJS资源未设置Content-Type

Jak*_*ake 6 javascript angularjs

我正在尝试编写一个AngularJS资源模块,它将一些数据发布到服务器.默认内容类型似乎是"application/xml".我试图将内容类型覆盖为"application/x-www-form-urlencoded".当做一个正常的$ http.post()我可以设置内容类型,当我签入Firebug时,我可以看到它设置正确.当我使用资源的POST方法时,我无法从默认值中更改内容类型.我认为我是根据文档描述的方式来做的.

http://jsfiddle.net/vBsUH/3/

var myApp = angular.module('myApp',['myResource']);

angular.module('myResource', ['ngResource']).factory('myResource', function($resource){
   return $resource('/echo/json',{},{
      add:{ method:'POST', params:{ foo:'1' }, headers:{'Content-Type':'application/x-www-form-urlencoded'} }
   });
});

function MyCtrl($scope,$http,myResource) {
   $scope.click = function() {

      //This will make an ajax call with a content-type of application/xml    
      myResource.add();

      //this will make an ajax call with a content-type of application/x-www-form-urlencoded
      $http.post('/echo/json','foo=1',{'headers':{'Content-Type':'application/x-www-form-urlencoded'}});

   }
}?
Run Code Online (Sandbox Code Playgroud)

关于如何使用不同的内容类型发布AngularJS资源的任何想法或示例将非常感激.

小智 1

杰克,昨天我为类似的问题提供了一些信息:/sf/answers/901374911/

将该响应粘贴到下面:

虽然开发文档(截至 10 月 12 日)显示可以在 $resource 中覆盖标头,但它尚未发布(v1.0.2 或 v1.1.0)。不过,该功能位于 v1.0.x 和 master 分支中。为了获得该功能,您现在可以考虑从 v1.0.x 分支进行构建。

如何构建:http://docs.angularjs.org/#H1_4

或者,您可以从快照构建中提取:http://code.angularjs.org/snapshot/

看起来这个功能将在下一个版本中出现。