AngularJS unexpected token in fromJson()
Asked Answered
S

2

18

The following line of code:

var sid = $cookieStore.get('PHPSESSID');

is throwing this error:

SyntaxError: Unexpected token m
at Object.parse (native)
at Object.fromJson (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:779:14)
at Object.angular.module.factory.factory.get (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-cookies.js:149:34)
at Object.getAllImages (https://7-september.com/photoslide/js/services.js:29:36)
at new Management (https://7-september.com/photoslide/js/controllers.js:54:18)
at invoke (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2902:28)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2914:23)
at $get (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:4805:24)
at update (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:14198:26)
at Object.$get.Scope.$broadcast (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:8307:28) 

So, I put a breakpoint at angular.js at line 779. The code there is:

777    function fromJson(json) {
778        return isString(json)
779            ? JSON.parse(json)
780            : json;
781    }

The value of json as passed into JSON.parse() is "mnp801fap6kor50trgv4cgk7m2".

I also noticed that other things getting passed into that method usually have two quotes around them, like ""userName"" for example.

Please help. I'm banging my head against this for a couple hours.

Or, if someone knows a better way to get the php session id out of the cookies using Angular, that would be great, too. (Yes, I could do it using jQuery or bare-metal JS, but I'd prefer to keep using Angular).

Thanks in advance!!!

Scarbrough answered 8/9, 2013 at 0:2 Comment(0)
M
23

It doesn't appear that $cookieStore is meant to read cookies that weren't created by $cookieStore. From the comments:

* @description
* Provides a key-value (string-object) storage, that is backed by session cookies.
* Objects put or retrieved from this storage are automatically serialized or
* deserialized by angular's toJson/fromJson.

The automatically serialized or deserialized is the important part. I was able to reproduce your problem here: http://jsfiddle.net/xtmrC/

You probably want to use $cookies instead, like so: http://jsfiddle.net/MXTY4/9/

angular.module('myApp', ['ngCookies']);
function CookieCtrl($scope, $cookies) {
    console.log('haha', $cookies.PHPSESSID);
}
Madelainemadeleine answered 8/9, 2013 at 0:40 Comment(3)
Thanks. I actually found the answer to this a few minutes after I posted it, but couldn't post the answer for eight hours because my rep isn't high enough. But thanks for the response. You are correct. It appears that in the process of adding something into the $cookieStore, it also adds an extra set of "" around the value. So, yeah, you can only get things out of $cookieStore that you put the yourself. ANd using $cookies.PHPSESSID worked great.Scarbrough
Also, I'd upvote your answer but I can't 'cause my rep isn't high enough.Scarbrough
I upvoted your question.. maybe now you can? Welcome to StackOverflow!Madelainemadeleine
P
0

Or you can simply wrap the $cookieStorage in a function and return a value or an empty string.

var sid = getPHPSESSID();

function getPHPSESSID() {
   return $cookieStore.get('PHPSESSID') || '';
}

In that case you'll use the angular service with it's pros and if you don't put the key-value pair in cookies, you'll get the empty string in result

Pentstemon answered 14/9, 2015 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.