Paulo
Paulo

Reputation: 1498

Setting cookies manually with jquery

I use the ajax method from jquery to send POST commands to another server. I have CorsE plugin set in my firefox and I can send POST commands, and it returns successfully. But, I'm trying to login with user and password, and it's everything ok, except the fact that the cookie value that I send with the ajax method is not the one the browser set to use.

One of the parameters is "headers", and I set cookie with the values I want, i.e., "valueA=bla; valueB=bla2", but when I inspect the network tab from inspect element, I see that firefox send the POST with another cookie, i.e., "valueA=tal; valueB=tal2" (I made them up).

How do I force that firefox uses the value that I set initially?

My ajax call is:

$.ajax({
        url: url,
        type: "POST",
        data: {
            "email": email,
            "pass": pass
        },
        headers: {
            "Host": "www.someexample.com",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0",
            "Accept-Language": "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
            "DNT": "1",
            "Connection": "keep-alive",
            "Content-Length": 47,
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded",
            "Referer": url,
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4",
            "Cookie": "PHPSESSID=abcdefgh; SERVERID=ookkuu;"
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            console.log(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log("Status: " + textStatus);
            console.log("Error: " + errorThrown);
        }

After sending, values of PHPSESSID and SERVERID are different from the values that they were previously set. I'd like to force firefox to send the same value.

Upvotes: 0

Views: 515

Answers (1)

Barmar
Barmar

Reputation: 780889

It will always send the cookies that are in document.cookies. If you want to override them, change that variable rather than trying to use a custom header. If you want it to be just for that one call, save the old value, assign the new cookies that you want, send the AJAX request, and then restore the old value.

Upvotes: 1

Related Questions