Laravel 4 Cookie的JSON响应 [英] Laravel 4 JSON Response with Cookie
本文介绍了Laravel 4 Cookie的JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设置带有json响应的cookie?
How can I set a cookie with a json response?
至少我注意到,以下命令是唯一设置cookie的方法:
I noticed, for me at least, the following command is the only thing working that sets a cookie:
return Redirect::to('/')
->withCookie(Cookie::make('blog', $cookie_values, 1000));
当然,如果它是ajax请求,它将返回重定向的目标.
Of course if it was an ajax request it would return the target of the redirect.
如何将其转换为ajax请求并返回包含cookie的json响应?
How could I translate this to an ajax request and return a json response with the cookie?
推荐答案
我能够使用以下代码通过json响应设置cookie:
I was able to set a cookie with a json response with the following code:
$cookie_values = array(
'name' => Input::get('name'),
'id' => Auth::user()->id,
'login_success' => 1);
if(Request::ajax())
{
$cookie = Cookie::make('blog', $cookie_values, 1000);
$response = Response::json($cookie_values);
$response->headers->setCookie($cookie);
return $response;
}
这篇关于Laravel 4 Cookie的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文