首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Webstorm中接收SyntaxError:意外的token /,而不是Chrome

在Webstorm中接收SyntaxError:意外的token /,而不是Chrome
EN

Stack Overflow用户
提问于 2016-08-30 06:26:25
回答 2查看 425关注 0票数 0

我正在做一个项目,我有下面的电话

代码语言:javascript
复制
try {this.set("config", JSON.parse(dijitConfig));}

使用dijitConfig =

代码语言:javascript
复制
"\r\n{\r\n    \"getStationUrl\" : \"http://localhost:6080/arcgis/rest/services/testcenterline/MapServer/exts/StationLocator/GetStation\",\t\r\n\t\"tolerance\" : 5,\r\n\t\"getStationInterval\" : 1\r\n}\n//# sourceURL=http://localhost:63342/StationLocator/Main/StationLocator%20JSAPI/Source/StationLocator%20JSAPI/js/StationLocatorConfig.js"

由于某些原因,当我通过Chrome运行时,应用程序启动时没有错误,在Webstorm内部,我收到以下异常:

代码语言:javascript
复制
SyntaxError: Unexpected token / in JSON at position 180

为什么这个JSON在Chrome中有效,但在Webstorm中无效?

请注意,在Webstorm中,我在Settings->Languages & Frameworks->JavaScript->Libraries中启用了HTML5/ECMAScript 5

EN

回答 2

Stack Overflow用户

发布于 2016-08-30 06:35:32

如果dijitConfig是javascript对象而不是字符串,则JSON.parse()将失败。您只能将字符串解析为对象。

如果您正在加载一个AJAX请求,那么Accept头可能不正确,服务器可能返回了错误的格式。我以前在Firefox中遇到过这种情况,AJAX请求特别需要将Accept头设置为application/json

票数 0
EN

Stack Overflow用户

发布于 2016-08-30 06:48:18

这是无效的JSON。例如,当我这样做时:

代码语言:javascript
复制
JSON.parse("\r\n{\r\n    \"getStationUrl\" : \"http://localhost:6080/arcgis/rest/services/testcenterline/MapServer/exts/StationLocator/GetStation\",\t\r\n\t\"tolerance\" : 5,\r\n\t\"getStationInterval\" : 1\r\n}\n//# sourceURL=http://localhost:63342/StationLocator/Main/StationLocator%20JSAPI/Source/StationLocator%20JSAPI/js/StationLocatorConfig.js")

我明白了:StationLocatorConfig.js:7 Uncaught SyntaxError: Unexpected token / in JSON at position 179

您需要确保您的配置是有效的JSON。尝试使用JSON linter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39216120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档