热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Laravel4JSON响应Cookie-Laravel4JSONResponsewithCookie

HowcanIsetacookiewithajsonresponse?如何设置带有json响应的cookie?Inoticed,formeatleast,thef

How can I set a COOKIE with a json response?

如何设置带有json响应的COOKIE?

I noticed, for me at least, the following command is the only thing working that sets a COOKIE:

我注意到,至少对我来说,以下命令是设置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.

当然,如果是ajax请求,它将返回重定向的目标。

How could I translate this to an ajax request and return a json response with the COOKIE?

我怎么能将它转换为ajax请求并返回带有COOKIE的json响应?

2 个解决方案

#1


3  

I was able to set a COOKIE with a json response with the following code:

我能够使用以下代码设置带有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;
            }

#2


1  

Great hint!

Having a look at Symfony\Component\HttpFoundation\ResponseHeaderBag also revealed how to set headers for a json response when having problems with HTTP access control:

看看Symfony \ Component \ HttpFoundation \ ResponseHeaderBag还揭示了如何在遇到HTTP访问控制问题时为json响应设置标头:

$response->headers->set('Access-Control-Allow-Origin', '/* your subdomain */');

推荐阅读
author-avatar
哒Dayling玲
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有