开发者

setRequestHeader + Cookie

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 c开发者_高级运维aontent-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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜