来自 CDN 的 javascript 中的意外非法令牌 - 脚本仅包含乱码 [英] Unexpected Illegal Token in javascript from CDN - scripts contains only garbled text

查看:15
本文介绍了来自 CDN 的 javascript 中的意外非法令牌 - 脚本仅包含乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 PC(运行 Windows 7)上托管一个网页,并打算使用 bootstrap css/javascript 库,但是我在使用来自 CDN 的 bootstrap javascript 库时遇到了问题.这是一个最小的例子,index.html

<html lang="zh-cn"><头><title>你好世界</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="样式表" ><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script><身体><div class="容器">你好,世界

</html>

我尝试使用python -m SimpleHTTPServer"和使用 node.js + express 在本地主机上托管页面,但我得到相同的结果,我收到消息Uncaught SyntaxError: Unexpected token ILLEGAL"并参考到 chrome 的 javascript/html 调试器中的 bootstrap.min.js:1 .如果我打开 bootstrap.min.js 它只显示很多中文符号(在谷歌翻译中被翻译成毫无意义的文本).所有这些也适用于其他 CDN 脚本,例如 socket.io.

*编辑 1:我尝试下载源代码并在本地加载 bootstrap.min.js 脚本与

<script src="/js/bootstrap.min.js"></script>

但我遇到了与以前完全相同的问题.

解决方案

我在自己的本地页面上遇到了同样的问题.我认为这可能是我加载 bootstrap 或 jquery 的方式有问题,因为这些文件在 Chrome 开发工具中显示看起来像中文的字符,即使从 URL 加载时 js 和 css 文件看起来没问题.谷歌搜索意外令牌非法"在 Stack Overflow 上提出了这个问题,所以我会回答.问题原来是我的 HTML 中的无效字符.

我的解决方法是从头开始创建一个死的简单页面,并从失败的页面中逐一添加功能,直到它与我的失败页面相似.最后,源在文本编辑器中看起来相同,但新构建的页面运行良好,而原始页面在 Chrome 控制台中出现意外令牌"错误.所以我比较了这些文件,发现了一些导致失败的隐藏字符(我从网页片段中复制并粘贴了这些字符).

当我将上面的 HTML 复制并粘贴到文本文件中并在 Chrome 中打开时,它显示正确.

I'm trying to host a webpage on my PC (Running Windows 7), and intend to use bootstrap css/javascript library, but I am having trouble using the bootstrap javascript library from a CDN. Here is a minimal example, index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Hello world</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" >
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
Hello world
</div>
</body>
</html>

I have tried to host the page on localhost using both "python -m SimpleHTTPServer" and using node.js + express, but I get the same result, where I get the message "Uncaught SyntaxError: Unexpected token ILLEGAL" with reference to bootstrap.min.js:1 in chrome's javascript/html debugger. If I open bootstrap.min.js it only shows lots of chinese signs (which is translated to meaningless text in google translate). All this goes for other CDN scripts also, such as socket.io.

*Edit 1: I tried downloading the source and loading the bootstrap.min.js script locally with

<script src="/js/bootstrap.min.js"></script>

but I get the excact same problem as before.

解决方案

I had the same problem on my own local page. I thought it might be an issue with the way I was loading bootstrap or jquery as these files were showing what looked like Chinese characters in Chrome Dev tools, even though the js and css files looked ok when loaded from a URL. Googling "Unexpected token ILLEGAL" came up with this question on Stack Overflow, so I'll answer. The issue turned out to be invalid characters in my HTML.

My method to solve was to create a dead simple page from scratch and add the features from the failing page one by one until it resembled my failing page. In the end, the source looked identical in a text editor, but the newly constructed page worked fine while and the original page had the "unexpected token" error in Chrome console. So I compared the files and found some hidden characters (which I had copy and pasted from a web page snippet) which were causing the failure.

When I copy and paste your HTML above into a text file and open it in Chrome, it displays correctly.

这篇关于来自 CDN 的 javascript 中的意外非法令牌 - 脚本仅包含乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆