Laravel 4 JSON响应Cookie(Laravel 4 JSON Response with Cookie)

编程入门 行业动态 更新时间:2024-10-23 22:28:19
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; }

更多推荐

本文发布于:2023-07-27 17:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   Laravel   Response   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!