javascript - fromJson() 中的 AngularJS 意外标记

标签 javascript json angularjs angularjs-service

以下代码行:

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/

相关文章:

angularjs - 简单的 Angular $routeProvider 解析测试。这段代码有什么问题?

javascript - 使用 requestAnim 或 TimeOut 在动画中滞后 FPS

javascript - 为什么 PhantomJS 会调用两次 onResourceReceived ?

javascript - asp.net 回发和 javascript/jquery

javascript - Coldfusion 的 cfpdfform 的 PHP 等价物

javascript - Angular JS Controller 未调用 Spring MVC

javascript - Function() 构造函数是否没有像 eval 那样被 V8 优化?

ruby - 类型错误:无法将 Net::HTTPOK 转换为字符串

c# - 如何将 json-patch 应用于 .net core 中的纯 json?

javascript - 如何删除值的格式化掩码