Trying to set cookie in ajax call, Laravel

瘦欲@ 提交于 2019-12-11 09:23:57

问题


I am trying to set a cookie in an ajax call, but can't get it to work. I have understood that I need to set it via setCookie on $response->headers. The $cookie variable looks correct, but no cookie is set in the browser.

Code from my controller:

$cookie = Cookie::make('testcookie', array('testvalue' => 'test'), 1000);  
$response = Response::json(array('status' => 'ok'));  
$response->headers->setCookie($cookie);  
return $response;

回答1:


I think you are calling this function after the page is initialized. So no cookie headers can be sent after the html tags started.

Refer to this Link

and this will help you



来源:https://stackoverflow.com/questions/22903995/trying-to-set-cookie-in-ajax-call-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!