dozxos6346
2016-05-26 09:22 阅读 67

Php Object to JSON返回奇怪的Json

I am working on a system where I retrieve user preferences and insert them into cookies with JSON because I have a Javascript file that needs to use the value in the cookie too.

Now, when i use JSON.Stringify() with javascript i get the data just as i expect it. enter image description here

However, when i go to check the cookie after it has run the PHP script i get this: enter image description here

Here is my code from the php file:

public function onSecurityInteractiveLogin(InteractiveLoginEvent $event){
    if($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY')){
        $selectedCategories = $event->getAuthenticationToken()->getUser()->getProfile()->getCategories();
        setcookie("userInterests",json_encode(get_object_vars($selectedCategories)),(time()+3*60*60*24*30));
    }
}

any help would be greatly appreciated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douwanc63652 douwanc63652 2016-05-31 08:30

    In my case I had multiple problems: firstly I tried to enter an object(that I thought was an array) into the cookie. This had the weird cookie as a result, because the PHP cookie only held the start string for the urlencode.

    Secondly, I didn't know that cookies automatically get urlencoded in PHP so that threw me of. If I just use urldecode now, at the point where I am returning the data from the cookie. It should just pass the javascript cookie without editing it and my php cookie will be useable.

    点赞 评论 复制链接分享

相关推荐