0

I need to access my requestHeaders in my onload function. How can I do that ?

Thanks

pmaruszczyk
  • 2,157
  • 2
  • 24
  • 49
allthenutsandbolts
  • 1,513
  • 1
  • 13
  • 34
  • 3
    Uhm... `requestHeaders` from `XHR` in your `onload` function? What do you mean? Just get the headers with which a page was loaded (so apart from XHR)? Or better, what is it you want to achieve? – Marcel Korpel May 10 '11 at 21:53
  • well I'm accessing a url which uses basic authentication. Once you log in,the site drops in some cookies. I don't see those cookies when I do getRequestHeader("Set-Cookie"). I see everything else except the required values. When I had written the same code in groovy, I had to access the request object and from there I was able to access the cookie. – allthenutsandbolts May 11 '11 at 01:30

2 Answers2

0

If you want to access cookies from within JavaScript you can read them using document.cookie:

allCookies = document.cookie;

allCookies is a string containing a semicolon-separated list of cookies (i.e. key=value pairs)

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • document.cookie will give me all the cookies which I have set.... it will not give me the cookies which may have dropped by a site in the request object.... – allthenutsandbolts May 11 '11 at 14:05
  • @all: But why do you need to access cookies that are no longer set? Again, what do you want to achieve? – Marcel Korpel May 11 '11 at 14:09
  • Here is what I'm trying to achieve.... I have a URL which I go to, the URL authenticates me using basic authentication which works fine. The moment you are authenticated, the site drops some cookies which can be used for subsequent logins. The problem is that. I do see those cookies in Developer Console but then I try to do getAllRepsonseHeader("Set-Cookie") I don't see the cookie which I'm looking also getAllResponseHeaders() is also not returning anything for that cookie. One more thing the Cookie has a domian associated with it. – allthenutsandbolts May 11 '11 at 20:20
  • If you are not seeing the cookies being set by the server, check for issues related to the path and domain being used to set the cookie, and compare with not-using-XHR to check where the actual problem is. See my answer above. – Tom Jun 10 '11 at 22:49
0

Basically cookies are managed for you in a browser environment. What this means is that any cookies set up by the server should appear on the client (even when using XHR) and should be sent up to the server with every subsequent request, (even when using XHR) - without any intervention.

If you do need to actually access the cookie, As Marcel said, you need to simply use document.cookie at the correct time after the XHR request has completed.

See this question too

Community
  • 1
  • 1
Tom
  • 7,994
  • 8
  • 45
  • 62