我正在做一个项目,我有下面的电话
try {this.set("config", JSON.parse(dijitConfig));}使用dijitConfig =
"\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内部,我收到以下异常:
SyntaxError: Unexpected token / in JSON at position 180为什么这个JSON在Chrome中有效,但在Webstorm中无效?
请注意,在Webstorm中,我在Settings->Languages & Frameworks->JavaScript->Libraries中启用了HTML5/ECMAScript 5
发布于 2016-08-30 06:35:32
如果dijitConfig是javascript对象而不是字符串,则JSON.parse()将失败。您只能将字符串解析为对象。
如果您正在加载一个AJAX请求,那么Accept头可能不正确,服务器可能返回了错误的格式。我以前在Firefox中遇到过这种情况,AJAX请求特别需要将Accept头设置为application/json。
发布于 2016-08-30 06:48:18
这是无效的JSON。例如,当我这样做时:
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。
https://stackoverflow.com/questions/39216120
复制相似问题