views:

18

answers:

1

Hello everyone,

my question is, how to send a cookie with setRequestHeader. My code to test it is this one:

var client = new XMLHttpRequest();
client.open('POST', 'url');
client.setRequestHeader('Cookie', 'test=mycookie');
client.setRequestHeader('Cookie', 'test=mycookie');
 alert("start");
 client.onreadystatechange = function() {
  if(client.readyState == 4 && client.status == 200){
   alert("beginning");
   alert(client.getAllResponseHeaders()); 
   document.getElementById('test').innerHTML = client.responseText;
   alert("end");
  }
 }
 client.send();

The getAllResponseHeaders()-method gives only caontent-type. But how I can see if the cookie is set or not?

So it must be javascript and document.cookie is no way, because i develope for the Nokia WRT and it doesn't use this.

Thanx for your help

A: 

XMLHttpRequest wasn't designed to be able to do what your asking it to do. As you rightly point out document.cookie is the usual way to set cookies in javascript, it doesn't really make sense to request a cookie via AJAX like this.

If you can't use document.cookie then pass your cookie parameters as post data and have your server side handler set the cookie in the response.

For example if your server side handler was a Java servlet:

Cookie myCookie = new Cookie("Name", "Value");
HttpServletResponse res = getContext().getResponse().addCookie(myCookie);
Ollie Edwards