简体   繁体   English

AngularJS资源未设置Content-Type

[英]AngularJS resource not setting Content-Type

I am trying to write an AngularJS resource module that will post some data to the server. 我正在尝试编写一个AngularJS资源模块,它将一些数据发布到服务器。 The default content-type appears to be "application/xml". 默认内容类型似乎是“application / xml”。 I am trying to override the content-type to "application/x-www-form-urlencoded". 我试图将内容类型覆盖为“application / x-www-form-urlencoded”。 When doing a normal $http.post() I can set the content-type and when I check in Firebug, I can see it set correctly. 当做一个正常的$ http.post()我可以设置内容类型,当我签入Firebug时,我可以看到它设置正确。 When I use the resource's POST method, I cannot get the content-type to be changed from the default. 当我使用资源的POST方法时,我无法从默认值中更改内容类型。 I think that I am doing it according to how the documentation describes. 我认为我是根据文档描述的方式来做的。

http://jsfiddle.net/vBsUH/3/ 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'}});

   }
}​

Any ideas or examples on how to get an AngularJS resource to post with a different content-type would be much appreciated. 关于如何使用不同的内容类型发布AngularJS资源的任何想法或示例将非常感激。

Jake, yesterday I provided some info to a similar question: https://stackoverflow.com/a/12876784/1367284 杰克,昨天我提供了一些类似问题的信息: https//stackoverflow.com/a/12876784/1367284

Pasting that response below: 粘贴下面的响应:

While the development docs (as of 12 Oct) show that overriding headers is possible in a $resource, it hasn't yet been released (v1.0.2 or v1.1.0). 虽然开发文档(截至10月12日)显示在$资源中可以覆盖标题,但尚未发布(v1.0.2或v1.1.0)。 The feature is in the v1.0.x and master branches, however. 但是,该功能位于v1.0.x和主分支中。 In order to get at that feature, you might consider building from the v1.0.x branch for now. 为了获得该功能,您可以考虑立即从v1.0.x分支构建。

How to build: http://docs.angularjs.org/#H1_4 如何建立: http//docs.angularjs.org/#H1_4

Alternatively, you could pull from the snapshot build: http://code.angularjs.org/snapshot/ 或者,您可以从快照构建中提取: http//code.angularjs.org/snapshot/

Looks like this feature will be in the next release. 看起来这个功能将在下一个版本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM