如何设置自定义页眉以$资源行动? [英] how to set custom headers with a $resource action?

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

问题描述

与HTTP $,我们可以这样做:

with $http, we can do this:

var config = { headers: { 'something': 'anything' } };          
$http.get('url/to/json', config)
    .success(function() {
        // do something…
    })

我想这样做同样以$资源引用(不工作):

i would like to do the same with a $resource reference (not working):

var config = { headers: { 'something': 'anything' } };
MyResource.get( 
    config,
    function() { // success
        // do something…
    }
); 

通过声明如下相应的服务:

with the corresponding service declared like this :

.factory('MyResource', function($resource){
    return $resource('url/to/json');
})

它不工作:配置对象转到的网址,而不是在HTTP标头。

it's not working : the config object goes to the url and not in the http headers.

有没有办法做到这一点?

Is there a way to do that ?

推荐答案

标题 $资源是因为现有AngularJS 1.1.1。请确保您使用正确的版本。

headers for $resource is available since AngularJS 1.1.1. Make sure you have correct version used.

格式

$resource('url/to/json', {}, {headers: { 'something': 'anything' }});

[祖马编辑]
以上看起来不正确。为$资源的第三个参数应该是不同的。这似乎更正确的对我说:

[edit by zuma] The above doesn't seem right. The third parameter to $resource should be a different. This seems more correct to me:

$resource('url/to/json', {}, {
    get: {
        method: 'GET',
        headers: { 'something': 'anything' }
    }
});

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

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