1

I need to do an HTTP GET request in JS, with a pre-definied cookie.

What would be the best way to to that?

Found that, with many ways to do an request, but found no one that supports cookies... HTTP GET request in JavaScript?

Community
  • 1
  • 1
communications
  • 145
  • 1
  • 10

2 Answers2

1
    if(!isset($_COOKIE["cookiename"])) {
        setcookie("cookiename","cookievalue");
    } else {
        // This is propably the 2nd call of that site (with cookie)
        // Here your code comes
    }
MagicSux
  • 382
  • 3
  • 11
0

You can't set cookies for a specific request, but you can edit cookies on the clientside if you set them as such.

This cookies script abstracts this aways a bit, because you only have access to the full string, containing all of the cookies in one via document.cookie, natively.

Depending on you want to do, you might be better off setting GET parameters instead.

MildlySerious
  • 8,750
  • 3
  • 28
  • 30
  • If a make an JS request, the cookies from the Browser will be used? This would solve my whole problem... I can only change the cookies from the website the JS is hosted on right? – communications Nov 06 '13 at 12:36
  • 1
    AJAX requests include all cookies your browser would normally send for that request, yes. – MildlySerious Nov 06 '13 at 12:36
  • Would this work with an request like this? new Ajax.Request( '/myurl', { method: 'get', parameters: { 'param1': 'value1'}, onSuccess: function(response){ alert(response.responseText); }, onFailure: function(){ alert('ERROR'); } }); – communications Nov 06 '13 at 12:38
  • This would send all cookies like normal and they would be accessible on the server. :) – MildlySerious Nov 06 '13 at 12:40