Javascript 不允许加载本地资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37690419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not allowed to load local resources
提问by Sparksido
When I try to link my html page to my javascript files in my express app, I get the error above in my developer tools. I am running this in an express app with an hbs rendering engine on my local machine. The code snippet looks like this
当我尝试将我的 html 页面链接到我的 Express 应用程序中的 javascript 文件时,我在我的开发人员工具中收到上述错误。我在本地机器上带有 hbs 渲染引擎的 express 应用程序中运行它。代码片段看起来像这样
html
html
<script type="text/javascript" src="file:///public/javascripts/jquery/jquery-2.2.4.min.js"></script>
<script type="/javascript" src="file:///public/javascripts/receiver.js"></script>
<script type="text/javascript" src="file:///public\javascripts\jquery-2.2.4.min.map"></script>
this is the part from app.js that sets the view engine
这是 app.js 中设置视图引擎的部分
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
and when I check the developer tools, I get this console error
当我检查开发人员工具时,我收到此控制台错误
Not allowed to load local resource: file:///public/javascripts/jquery/jquery-2.2.4.js (index) :1 Not allowed to load local resource: file:///public/javascripts/receiver.js (index) :1 Not allowed to load local resource: file:///public/javascripts/jquery-2.2.4.min.map
不允许加载本地资源:file:///public/javascripts/jquery/jquery-2.2.4.js (index) :1 不允许加载本地资源:file:///public/javascripts/receiver.js ( index) :1 不允许加载本地资源:file:///public/javascripts/jquery-2.2.4.min.map
Why does the program not access my javascripts?
为什么程序不能访问我的 javascripts?
回答by Phil
First.
第一的。
Do not load files with file://. Just enter the relative path.
Do not load javascript source maps via the script tag.
You can add this line at the and of the javascript file:
//# sourceMappingURL=/path/to/script.js.map
不要加载带有file:// 的文件。只需输入相对路径。不要通过脚本标签加载 javascript 源映射。您可以在 javascript 文件的 和 添加此行:
//# sourceMappingURL=/path/to/script.js.map
Second.
第二。
You have this little line of code in you express app: app.use(express.static('public'));
. If not, add it.
The server knows the public path. So you don′t have to write public/
在你表达应用程序,你有代码这个小行:app.use(express.static('public'));
。如果没有,请添加它。服务器知道公共路径。所以你不必写public/
If this is your structure:
如果这是您的结构:
- server.js
- public/
- index.html
- javascripts/
- receiver.js
- jquery/
- jquery-2.2.4.min.js
- 服务器.js
- 民众/
- 索引.html
- javascripts/
- 接收器.js
- jQuery/
- jquery-2.2.4.min.js
Write this into your html or handlebars file.
将其写入您的 html 或把手文件中。
<script type="text/javascript" src="javascripts/jquery/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="javascripts/receiver.js"></script>