Razor + JS Uncaught SyntaxError:无效或意外的令牌
Razor + JS Uncaught SyntaxError: Invalid or unexpected token
我有这样的代码:
var tempPath = "@Path.GetTempPath()";
var blobURL = tempPath + "image.jpg";
当我检查我的页面时,我得到这个错误:
未捕获SyntaxError:无效或意外的令牌
这一行下划线
var tempPath = "C:'Windows'TEMP'";
这可能是一个愚蠢的解决方案,但我似乎找不到它。我希望我的blob-URL为C:'Windows'TEMP'image.png,这样我就可以像这样使用它:
<img src="C:'Windows'TEMP'image.png"/>
编辑:我不想硬核这个临时路径,因为我不知道它是否总是相同的。
下面的JavaScript是无效的:
var tempPath = "C:'Windows'TEMP'"; // look at code highlighting
console.log(tempPath); // and at the result of execution
问题是'
反斜杠应该在JS字符串中转义。
您需要这样转义反斜杠:
var tempPath = "C:''Windows''TEMP''";
console.log(tempPath);
为了使用Razor实现此目的,您可以执行以下操作:
var tempPath = "@HttpUtility.JavaScriptStringEncode(Path.GetTempPath())";
相关文章:
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- jQuery$.getJSON抛出意外令牌
- 为什么我在下面的..of循环中得到意外令牌
- 组件中的ReactJS意外令牌
- AngularJS $http GET 请求到本地 JSON 文件返回语法错误:意外令牌]
- 巴别塔,意外令牌 (15:33) ::.
- Sencha 2.0 和 Codeigniter RESTful API 生成未捕获的语法错误:意外令牌:
- 在事件上使用 jquery 时出现意外令牌
- JSON 中位置 4 处的意外令牌<
- 意外令牌 ) 在我的代码中
- Electron 和 Babel 6 异步/等待抛出意外令牌
- “JSON.parse(str)”返回“意外令牌c”,但str是一个字符串
- jQuery after() uncatch SyntaxError: 意外令牌 ILLEGAL
- 使用节点javascript的意外令牌ILLEGAL
- Javascript/JQuery-Chrome意外令牌
- Ajax 帖子未捕获语法错误:意外令牌非法
- angularjs-SyntaxError:请求从服务器返回时出现意外令牌
- jquery:使用ember的意外令牌C
- React本机项目中的意外令牌
- 解析有效的 JSON 得到“语法错误:意外令牌非法”