AngularJS 资源未设置 Content-Type [英] AngularJS resource not setting Content-Type

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

问题描述

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

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

var myApp = angular.module('myApp',['myResource']);angular.module('myResource', ['ngResource']).factory('myResource', function($resource){return $resource('/echo/json',{},{添加:{ method:'POST', params:{ foo:'1' }, headers:{'Content-Type':'application/x-www-form-urlencoded'} }});});函数 MyCtrl($scope,$http,myResource) {$scope.click = function() {//这将使用 application/xml 的内容类型进行 ajax 调用myResource.add();//这将使用 application/x-www-form-urlencoded 的内容类型进行 ajax 调用$http.post('/echo/json','foo=1',{'headers':{'Content-Type':'application/x-www-form-urlencoded'}});}}

关于如何让 AngularJS 资源以不同的内容类型发布的任何想法或示例将不胜感激.

解决方案

Jake,昨天我为类似问题提供了一些信息:https://stackoverflow.com/a/12876784/1367284

在下面粘贴该回复:

虽然开发文档(截至 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/

看来此功能将在下一个版本中提供.

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

   }
}​

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

解决方案

Jake, yesterday I provided some info to a similar question: 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). 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.

How to build: http://docs.angularjs.org/#H1_4

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

Looks like this feature will be in the next release.

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

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