ITKeyword,专注技术干货聚合推荐

注册 | 登录

javascript - How to access ETag header with jQuery AJAX request?

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

I'm using a jQuery ajax call to request data from a server that is sending an ETag in the HTTP response headers. I need access to the header, but when the request succeeds and I call jqXHR.getAllResponseHeaders(), I only see a subset of the headers returned by the server.

Example:

var jqXHR = $.ajax({
        type: 'GET',
        url: <my api url>,
        dataType: 'json',
        ifModified: true,
        success: function (result) {
          var headers = jqXHR.getAllResponseHeaders();
          console.log(JSON.stringify(headers));
        });

The headers I see from the jqxhr are:

Pragma: no-cache\r\n
Last-Modified: Wed, 22 Jan 2014 10:45:14 +0000\r\n
Content-Type: text/html\r\n
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, no-cache=\"set-cookie\"\r\n
Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n

The actual headers returned from the server (observed in chrome dev tools):

Access-Control-Allow-Origin:*
Cache-Control:no-cache="set-cookie"
Cache-Control:post-check=0, pre-check=0
Cache-Control:no-store, no-cache, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Length:407
Content-Type:text/html
Date:Fri, 24 Jan 2014 20:27:54 GMT
ETag:"29d8d1d98115057fe902b520199ea1b3"
Expires:Sat, 26 Jul 1997 05:00:00 GMT
Last-Modified:Thu, 23 Jan 2014 07:14:57 +0000
Pragma:no-cache
Server:nginx/1.1.19
Set-Cookie:AWSELB=F3E9557318EB956CA386FC6CB4270164AD7830493699A2B6AED008F4C5F9DB5952A2A1072C33DDC32DEDE0CA6A3734EBAFD51B57A7A093B69A36A6659EF493E1B92BA63DE6;PATH=/
X-Powered-By:PHP/5.4.19

I need to access the ETag header, but it seems as if jQuery or chrome are hiding it from me. I have tried the same code in Firefox with the same results. Can someone help me with this?

javascript jquery ajax http-headers etag
|
  this question
asked Jan 24 '14 at 20:34 Swaraj 422 6 14

 | 

2 Answers
2

You can try accessing headers in complete callback instead of success

complete: function(XMLHttpRequest, textStatus){
   var eTag = XMLHttpRequest.getResponseHeader('ETag');
}

This seems to work for some users here


|
  this answer
answered Jan 24 '14 at 20:41 Mohammad Adil 34.4k 11 54 87 1   Just tried this, no eTag there either. –  Swaraj Jan 24 '14 at 20:59

 | 

Did you find this question interesting? Try our newsletter

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Subscribed! Success! Please click the link in the confirmation email to activate your subscription.

This could be a CORS issue. I was having the same problem, I saw the headers in Chrome - however my code could not access it.

I have ASP.Net Web API for my back-end and under the <system.webServer> node had to add:

<httpProtocol>
    <customHeaders>
          <add name="Access-Control-Expose-Headers" value="ETag, Retry-After"/>
    </customHeaders>
</httpProtocol>

I realized this after reading the following post: Etag header not returned from jQuery.ajax() cross-origin XHR


|
  this answer
answered Aug 15 '16 at 19:34 Sean 140 9

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。