以下代码行:
var sid = $cookieStore.get('PHPSESSID');
正在抛出此错误:
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)
所以,我在 angular.js 的第 779 行设置了一个断点。代码如下:
777 function fromJson(json) {
778 return isString(json)
779 ? JSON.parse(json)
780 : json;
781 }
json
的值传递到 JSON.parse()
是“mnp801fap6kor50trgv4cgk7m2”。我还注意到传递给该方法的其他内容通常有两个引号,例如“”用户名“”。
请帮忙。我用头撞了几个小时。
或者,如果有人知道使用 Angular 从 cookie 中获取 php session id 的更好方法,那也很棒。 (是的,我可以使用 jQuery 或裸机 JS 来做到这一点,但我更愿意继续使用 Angular)。
提前致谢!!!
最佳答案
$cookieStore
似乎没有旨在读取不是由 $cookieStore
创建的 cookie .从评论:
* @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.
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);
}
关于javascript - fromJson() 中的 AngularJS 意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679094/