【问题标题】:Cannot load modules with traceur - tries to access file via xhr无法使用 traceur 加载模块 - 尝试通过 xhr 访问文件
【发布时间】:2015-08-02 17:52:02
【问题描述】:

我正在尝试使用跟踪器。但是,当我想加载一个模块时,我收到一个错误,指出它不成功。我的代码基于其关于模块的文档中提供的示例跟踪器。

这是main.html:

<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script>
     System.traceurOptions = { experimental: true } 
</script>

<script type="module" src="ProfileView.js"></script>

以及加载的模块:

// ProfileView.js
import {firstName, lastName, year} from './Profile.js';

function setHeader(element) {
  element.textContent = firstName + ' ' + lastName;
}
// rest of module

// Profile.js
export var firstName = 'David';
export var lastName = 'Belle';
export var year = 1973;

我在 Chrome 中收到以下错误:

XMLHttpRequest 无法加载 file:///C:/Code/Tests/Traceur/ProfileView.js。跨源请求 仅支持协议方案:http、data、chrome、 铬扩展,https,铬扩展资源。

WebPageTranscoder 加载失败 file:///C:/Code/Tests/Traceur/ProfileView.js

未捕获的网络错误:无法在“XMLHttpRequest”上执行“发送”: 无法加载“file:///C:/Code/Tests/Traceur/ProfileView.js”。

我知道你不能通过文件系统发出 xhr 请求,但我看过一些教程,其中代码的结构与我的相似,并且可以在那里工作......

我可能做错了什么?

【问题讨论】:

  • 那些教程可能希望在网络服务器上运行(即使是本地服务器)。

标签: javascript xmlhttprequest cross-domain traceur


【解决方案1】:

您无法使用 xhr 访问文件系统,您应该通过运行本地网络服务器通过 http 打开这些页面。如果你真的想启用文件系统访问,你可以:http://www.chrome-allow-file-access-from-file.com/

即你启动 chrome chrome.exe --allow-file-access-from-files

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 2017-01-20
    • 2013-07-23
    • 1970-01-01
    • 2020-04-30
    • 2020-01-10
    • 2017-10-16
    相关资源
    最近更新 更多