使用$ resource发送ETag

Tre*_*ack 5 rest angularjs angular-resource

使用Angular JS的$ resource服务,有没有办法为资源的第二,第三等投票指定If-None-Match和ETag标头?

    var SummarySearch = $resource(<<apiurl>>,
        { },
        {
            get: {
                method: 'GET',
                headers: {
                    'x-header': 'id'
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

我已经得到这个来设置一个常量标题(这只是x-header: id在这种情况下),但我需要根据我上次看到的ETag的每个请求而变化的东西.

更新:

transformRequest看起来很有希望,但我无法访问我发起请求的数据,或者数据最终的URL.据我所知,我只能访问我正在POST的主体(在我做GET时未定义)和headersGetter函数.

Tre*_*ack 4

我发现 $resource 自己处理 ETag。

当我发布这个问题时,我还没有实现服务器端的东西。我设置了 If-None-Match 处理服务器端,认为我必须在客户端上使用 $http 服务,但使用我现有的 $resource .get 调用进行了尝试,它处理设置 If-None-Match在我第一次轮询资源后自动标题。

我正在重用 $resource 在设置过程中返回的 1 个实例。

编辑

我有真正的 jQuery 而不是 jqLit​​e。这就是 If-None-Match 似乎被设置的地方。

    // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
    if ( s.ifModified ) {
        ifModifiedKey = ifModifiedKey || s.url;
        if ( jQuery.lastModified[ ifModifiedKey ] ) {
            jqXHR.setRequestHeader( "If-Modified-Since", jQuery.lastModified[ ifModifiedKey ] );
        }
        if ( jQuery.etag[ ifModifiedKey ] ) {
            jqXHR.setRequestHeader( "If-None-Match", jQuery.etag[ ifModifiedKey ] );
        }
    }
Run Code Online (Sandbox Code Playgroud)