Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS unexpected token in fromJson()

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!!!

like image 995
cutmancometh Avatar asked Sep 08 '13 00:09

cutmancometh


1 Answers

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);
}
like image 130
Langdon Avatar answered Nov 01 '22 12:11

Langdon