如何指定自定义角$资源行动头参数 [英] How to specify headers parameter for custom Angular $resource action

查看:101
本文介绍了如何指定自定义角$资源行动头参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下工作正常,但我想这会修改$ httpProvider全球范围内,这是不是我想要的。

The following works fine, but I am thinking this modifies the $httpProvider globally, which isn't what I want.

angular.module('SessionService', ['ngResource'])
    .config(function($httpProvider){
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    })
    .factory('Login', function($resource){
        var resource = $resource('/adminui/login',{},{
            post:{
                method:"POST",
                isArray:false
            },
        });
        return resource;
    })
LoginCtrl = function($scope,Login) {
    $scope.login = function(){
        Login.post($.param({user:$scope.user.username,password:$scope.user.password}),$.noop,$.noop)
    }
}

反正有做到这一点呢?

Is there anyway to do this instead?

...
    .factory('Login', function($resource){
        var resource = $resource('/adminui/login',{},{
            post:{
                method:"POST",
                isArray:false,
                headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} // ignored
            },
        });
        return resource;
    })

在标题参数似乎被忽略。请求仍然

The "headers" parameter seems to be ignored. the request is still

内容类型:应用程序/ JSON的;字符集= UTF-8

是我的头值好吗?

推荐答案

虽然开发文档(12 10月2日)显示,覆盖头部可能在$资源,它尚未被释放(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/snapshot/

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

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

Looks like this feature will be in the next release.

这篇关于如何指定自定义角$资源行动头参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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