不允许加载本地资源 [英] Not allowed to load local resources
问题描述
当我尝试将我的html页面链接到我的快递应用程序中的javascript文件时,我在开发人员工具中收到了上述错误。
我在我的本地机器上使用hbs渲染引擎的快速应用程序中运行它。
代码片段如下所示
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
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>
这是来自app的部分设置视图引擎的.js
this is the part from app.js that sets the view engine
// 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
不允许加载本地资源:file:/// public / javascripts / jquery / jquery -2.2.4.js
(索引)
:1不允许加载本地资源:file:///public/javascripts/receiver.js
(index)
: 1不允许加载本地资源:file:///public/javascripts/jquery-2.2.4.min.map
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
为什么程序没有访问我的javascripts吗?
Why does the program not access my javascripts?
推荐答案
首先。
不要使用 file:// 加载文件。只需输入相对路径即可。
不要通过脚本标记加载javascript源地图。
您可以在和javascript文件中添加此行:
//#sourceMappingURL = / path / to / script.js.map
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
第二。
你在快递应用程序中有这么一小段代码: app 。使用(express.static( '公共'));
。如果没有,请添加它。
服务器知道公共路径。所以你不必写 public /
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/
如果这是你的结构:
- server.js
- public /
- index.html
- javascripts /
- receiver.js
- jquery /
- jquery-2.2.4.min.js
- server.js
- public/
- index.html
- javascripts/
- receiver.js
- jquery/
- jquery-2.2.4.min.js
将此内容写入您的html或把手文件。
Write this into your html or handlebars file.
<script type="text/javascript" src="javascripts/jquery/jquery-2.2.4.min.js"></script> <script type="text/javascript" src="javascripts/receiver.js"></script>
这篇关于不允许加载本地资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!