javascript AngularJS 资源未设置 Content-Type
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12785386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS resource not setting Content-Type
提问by Jake
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 documentationdescribes.
我正在尝试编写一个 AngularJS 资源模块,它将一些数据发布到服务器。默认的内容类型似乎是“application/xml”。我试图将内容类型覆盖为“application/x-www-form-urlencoded”。在执行普通的 $http.post() 时,我可以设置内容类型,当我检查 Firebug 时,我可以看到它设置正确。当我使用资源的 POST 方法时,我无法从默认值更改内容类型。我认为我是根据文档描述的方式来做的。
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 资源以不同内容类型发布的想法或示例将不胜感激。
采纳答案by DSK
Jake, yesterday I provided some info to a similar question: https://stackoverflow.com/a/12876784/1367284
Hyman,昨天我为类似问题提供了一些信息: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.
虽然开发文档(截至 10 月 12 日)表明可以在 $resource 中覆盖标头,但它尚未发布(v1.0.2 或 v1.1.0)。但是,该功能在 v1.0.x 和 master 分支中。为了获得该功能,您现在可以考虑从 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.
看起来此功能将在下一个版本中提供。