javascript fromJson() 中的 AngularJS 意外标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18679094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:51:16  来源:igfitidea点击:

AngularJS unexpected token in fromJson()

javascriptjsonangularjsangularjs-service

提问by cutmancometh

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 jsonas passed into JSON.parse()is "mnp801fap6kor50trgv4cgk7m2".

json作为传递到JSON.parse()是“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. (Yes, I could do it using jQuery or bare-metal JS, but I'd prefer to keep using Angular).

或者,如果有人知道使用 Angular 从 cookie 中获取 php 会话 ID 的更好方法,那也太棒了。(是的,我可以使用 jQuery 或裸机 JS 来完成,但我更愿意继续使用 Angular)。

Thanks in advance!!!

提前致谢!!!

回答by Langdon

It doesn't appear that $cookieStoreis meant to read cookies that weren't created by $cookieStore. From the comments:

它似乎不是$cookieStore为了读取不是由$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.

The automatically serialized or deserializedis the important part. I was able to reproduce your problem here: http://jsfiddle.net/xtmrC/

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

You probably want to use $cookiesinstead, 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);
}

回答by Julia Usanova

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

在这种情况下,您将使用具有优点的 angular 服务,如果您不将键值对放在 cookie 中,您将获得空字符串结果