AngularJS资源不设置内容类型 [英] AngularJS resource not setting Content-Type

查看:136
本文介绍了AngularJS资源不设置内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个AngularJS资源模块,将一些数据上传到服务器。默认的内容类型似乎是应用/ XML。我试图覆盖内容类型为应用程序/ x-WWW的形式urlen codeD。在做一个正常的$ http.post()我可以设置内容类型,当我在Firebug检查,我可以看到它正确设置。当我使用资源的POST方法,我不能得到内容类型,从默认的改变。我认为我根据文档如何描述这样做。

I am trying to write an AngularJS resource module that will post some data to the server. The default content-type appears to be "application/xml". I am trying to override the content-type to "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. When I use the resource's POST method, I cannot get the content-type to be changed from the default. I think that I am doing it according to how the documentation describes.

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'}});

   }
}​

这是如何获得AngularJS资源发布与不同的内容类型任何意见或例子是多少AP preciated。

Any ideas or examples on how to get an AngularJS resource to post with a different content-type would be much appreciated.

推荐答案

杰克,昨天我提供了一些信息,以一个类似的问题:<一href=\"http://stackoverflow.com/a/12876784/1367284\">http://stackoverflow.com/a/12876784/1367284

Jake, yesterday I provided some info to a similar question: http://stackoverflow.com/a/12876784/1367284

粘贴该响应如下:

虽然开发文档(如10月12日的)表明,覆盖头是可能的$资源,它尚未被释放(V1.0.2或V1.1.0)。该功能在v1.0.x和主分支,但是。为了获得该功能,您可以考虑从v1.0.x分公司建设了。

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). The feature is in the v1.0.x and master branches, however. In order to get at that feature, you might consider building from the v1.0.x branch for now.

如何打造: http://docs.angularjs.org/#H1_4

另外,你可以从快照生成拉: HTTP://$c$c.angularjs .ORG /快照/

Alternatively, you could pull from the snapshot build: http://code.angularjs.org/snapshot/

貌似这个功能将在下一版本中。

Looks like this feature will be in the next release.

这篇关于AngularJS资源不设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆