Laravel 4 JSON响应Cookie(Laravel 4 JSON Response with Cookie)
如何设置带有json响应的cookie?
我注意到,至少对我来说,以下命令是设置cookie的唯一工作:
return Redirect::to('/') ->withCookie(Cookie::make('blog', $cookie_values, 1000));当然,如果是ajax请求,它将返回重定向的目标。
我怎么能将它转换为ajax请求并返回带有cookie的json响应?
How can I set a cookie with a json response?
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));Of course if it was an ajax request it would return the target of the redirect.
How could I translate this to an ajax request and return a json response with the cookie?
最满意答案
我能够使用以下代码设置带有json响应的cookie:
$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; }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; }更多推荐
发布评论