![](/img/trans.png)
[英]How can I set the content-type as application/json for DELETE method in $resource AngularJS
[英]AngularJS resource not setting Content-Type
我正在尝试编写一个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'}});
}
}
关于如何使用不同的内容类型发布AngularJS资源的任何想法或示例将非常感激。
杰克,昨天我提供了一些类似问题的信息: https : //stackoverflow.com/a/12876784/1367284
粘贴下面的响应:
虽然开发文档(截至10月12日)显示在$资源中可以覆盖标题,但尚未发布(v1.0.2或v1.1.0)。 但是,该功能位于v1.0.x和主分支中。 为了获得该功能,您可以考虑立即从v1.0.x分支构建。
如何建立: http : //docs.angularjs.org/#H1_4
或者,您可以从快照构建中提取: http : //code.angularjs.org/snapshot/
看起来这个功能将在下一个版本中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.