接收SyntaxError:意外令牌/ JSON在Webstorm,但不是Chrome



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

try {this.set("config", JSON.parse(dijitConfig));}

with dijitConfig =

"rn{rn    "getStationUrl" : "http://localhost:6080/arcgis/rest/services/testcenterline/MapServer/exts/StationLocator/GetStation",trnt"tolerance" : 5,rnt"getStationInterval" : 1rn}n//# sourceURL=http://localhost:63342/StationLocator/Main/StationLocator%20JSAPI/Source/StationLocator%20JSAPI/js/StationLocatorConfig.js"

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

SyntaxError: Unexpected token / in JSON at position 180

为什么这个JSON在Chrome中有效,而不是Webstorm?

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

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

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

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

JSON.parse("rn{rn    "getStationUrl" : "http://localhost:6080/arcgis/rest/services/testcenterline/MapServer/exts/StationLocator/GetStation",trnt"tolerance" : 5,rnt"getStationInterval" : 1rn}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。

最新更新