简体   繁体   English

fromJson()中的AngularJS意外标记

[英]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: 所以,我在angular.js的第779行放了一个断点。那里的代码是:

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". 传入JSON.parse()json值是“mnp801fap6kor50trgv4cgk7m2”。

I also noticed that other things getting passed into that method usually have two quotes around them, like ""userName"" for example. 我还注意到,传递给该方法的其他东西通常有两个引号,例如“”userName“”。

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. 或者,如果有人知道使用Angular从cookie中获取php会话ID的更好方法,那也会很棒。 (Yes, I could do it using jQuery or bare-metal JS, but I'd prefer to keep using Angular). (是的,我可以使用jQuery或裸机JS来做,但我更喜欢继续使用Angular)。

Thanks in advance!!! 提前致谢!!!

It doesn't appear that $cookieStore is meant to read cookies that weren't created by $cookieStore . 似乎$cookieStore不是为了读取$cookieStore不创建的$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. automatically serialized or deserialized是重要的部分。 I was able to reproduce your problem here: http://jsfiddle.net/xtmrC/ 我能够在这里重现你的问题: http//jsfiddle.net/xtmrC/

You probably want to use $cookies instead, like so: http://jsfiddle.net/MXTY4/9/ 您可能想要使用$cookies ,如下所示: http//jsfiddle.net/MXTY4/9/

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

Or you can simply wrap the $cookieStorage in a function and return a value or an empty string. 或者你可以简单地将$ cookieStorage包装在一个函数中并返回一个值或一个空字符串。

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 在这种情况下,你将使用角度服务与它的专业人士,如果你没有把键值对放在cookie中,你将获得结果中的空字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM