EpicWally
EpicWally

Reputation: 297

Laravel Setting cookies with Ajax

I am having trouble with setting cookies in laravel via ajax. I have read a number of questions/posts to no avail.

I have a drop down, that when changed, uses js to post its value to a controller which sets the cookie and returns a response with that cookie, then the page is refreshed upon ajax completion, and in the document.ready script, it calls a get cookie route, which retrieves the value from that cookie. it appears the cookie isn't being sent? I know the get path works, as if I set the cookie elsewhere it retrieves it properly. I have also used headers_sent() to ensure that headers haven't already been sent prior to setting the cookie and returning the response. I have also tried a number of different approaches that I have seen in other SO questions and answers, including Cookie::queue, $response->withCookie($cookie) and $response->headers->setCookie($cookie), none of which I've found to work.

Is there a problem with my approach? or is it more likely a syntax problem?

My javascript:

function trans(lang){
    $.post( 'cookie/set' , {'name':'language', 'value':lang, 'timeout':-1})
    .done(function(data){
        removeEvent();
    }).always(function(){
        window.location.reload();
    });
}

Controller function for cookie/set:

public function postSet(){
    $name = Input::get('name');
    $value = Input::get('value');
    $timeout = Input::get('timeout');
    if(Request::ajax()){
        Log::info('cookie', array('name'=>$name, 'value'=>$value, 'timeout'=>$timeout));
        $cookie = Cookie::make($name, $value, $timeout);
        $response = Response::make();
        $response->headers->setCookie($cookie);
        return $response->withCookie($cookie);
    }
}

doc.ready javascript:

$(document).ready(function(){
    $.post( 'cookie/get' , {'name':'language'})
    .done(function(data){
        if (!data){
            alert('no cookie');
        }
        $("#language").val(data);
        removeEvent();
    });
})

and finally the cookie/get controller function:

public function postGet(){
    $name = Input::get('name');
    define('__TRANSLATEMODE',"raw");
    return Cookie::get($name);
}

Again, I can confirm the get route works correctly. My thought is that somehow the cookie isn't being returned with the response from the set ajax call, but I don't understand why.

Thanks in advance for your help! -Wally

EDIT:

I looked at the request and responses in firebug, and it appears that the response from the postSet route does not contain the cookie. I had somewhat suspected this. but at least this nails down the source of the problem. Still not sure why it doesn't want to work though.

Edit 2:

I solved it. Not fully sure why this works, but the code below now works. It seems that returning an empty response would not send cookies with it? I dunno, it's solved at least. I will answer this when it will let me, and mark it as accepted. (If anyone knows WHY this fixed it, comments would be greatly appreciated.)

public function postSet(){
    $name = Input::get('name');
    $value = Input::get('value');
    $timeout = Input::get('timeout');
    Cookie::queue($name, $value, $timeout);
    $response = Response::make();
    return Response::make('test');
}

Thanks to those who looked, and hopefully this helps someone else. -Wally

Upvotes: 3

Views: 4589

Answers (2)

EpicWally
EpicWally

Reputation: 297

I solved it. Not fully sure why this works, but the code below now works. It seems that returning an empty response would not send cookies with it? I dunno, it's solved at least. I will answer this when it will let me, and mark it as accepted. (If anyone knows WHY this fixed it, comments would be greatly appreciated.)

public function postSet(){
    $name = Input::get('name');
    $value = Input::get('value');
    $timeout = Input::get('timeout');
    Cookie::queue($name, $value, $timeout);
    $response = Response::make();
    return Response::make('test');
}

Thanks to those who looked, and hopefully this helps someone else. -Wally

Upvotes: 1

Keith Mifsud
Keith Mifsud

Reputation: 1618

I can see you have a js Post function with a Get Route. This will not work in Laravel.

Not sure if this the entire issue but checking the routes will defintely help you narrow it down.

Upvotes: 1

Related Questions