fromJson()中的AngularJS意外标记

cut*_*eth 18 javascript json angularjs angularjs-service

以下代码行:

var sid = $cookieStore.get('PHPSESSID');
Run Code Online (Sandbox Code Playgroud)

抛出这个错误:

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) 
Run Code Online (Sandbox Code Playgroud)

所以,我在angular.js的第779行放了一个断点.那里的代码是:

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

json传入的值JSON.parse()是"mnp801fap6kor50trgv4cgk7m2".

我还注意到,传递给该方法的其他内容通常会有两个引号,例如"userName"".

请帮忙.我几个小时就撞了这个头.

或者,如果有人知道使用Angular从cookie中获取php会话ID的更好方法,那也会很棒.(是的,我可以使用jQuery或裸机JS来做,但我更喜欢继续使用Angular).

提前致谢!!!

Lan*_*don 23

它似乎不是$cookieStore为了读取非创建的cookie $cookieStore.来自评论:

* @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.
Run Code Online (Sandbox Code Playgroud)

automatically serialized or deserialized是重要的部分.我能够在这里重现你的问题:http://jsfiddle.net/xtmrC/

您可能想要使用$cookies,如下所示:http://jsfiddle.net/MXTY4/9/

angular.module('myApp', ['ngCookies']);
function CookieCtrl($scope, $cookies) {
    console.log('haha', $cookies.PHPSESSID);
}
Run Code Online (Sandbox Code Playgroud)