Search code examples
authenticationcookiesxmlhttprequestrequest-headershttp-options-method

how can i set document.cookie in ajax?


the error i get

OPTIONS https://page/ net::ERR_EMPTY_RESPONSE

and the reason i do need to use a RequestHeader("Cookies", "test");

Login Failure: Browser did not support Cookie. Please enable Cookie.

what can i do to get around this ? like how does the browser send a header while options isn't allowed ? sending a get request before the post request didn't help because there wasn't any response cookies .

some lines from the page source code that i think are important .

var strCookie = document.cookie;
document.cookie = cookie;
var cookie = "Language=en" + expires + "; 
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

even when i bypassed that error by running the code in the page console chrome tab ... still

Please enable Cookie

so i tried

 var cookie = "Language=en"
document.cookie = cookie;

and it worked but in the js file it didn't even

console.log(document.cookie)

returns empty ! the full code

var cookie = "Language=en"
document.cookie = cookie;
console.log(document.cookie)
var xhr1 = new XMLHttpRequest();
xhr1.open("post", 'https://page', false);
xhr1.onreadystatechange = function() {
if (xhr1.readyState === XMLHttpRequest.DONE && xhr1.status === 200) {
var cookie = "Language=en"
document.cookie = cookie;
console.log(document.cookie)
}
}
xhr1.send("Username=test&Password=test");

Solution

  • the script must run on the same url/ip that you want to set the cookies on … you can’t set cookies for other sites simply using document.cookie = "cookie"; maybe using something like this you can have two different ips in one url