我正在尝试学习react.js,但被困在“ Hello World”脚本中。
我的index.html:
<!DOCTYPE html> <html> <head> <script src="https://fb.me/react-0.13.3.js"></script> <script src="https://fb.me/JSXTransformer-0.13.3.js"></script> </head> <body> <div id="example"></div> <script type="text/jsx" src="src/helloworld.js"></script> </body> </html>
和src / helloworld.js:
React.render( <h1>Hello, world!</h1>, document.getElementById('example') );
当我把这个代码里面<script>的index.html文件,它工作正常,但是当我将其移动到单独的文件中我得到的空白页,并控制台错误:
<script>
index.html
XMLHttpRequest cannot load file:///home/marcin/Projects/react/src/helloworld.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
怎么了
您收到该错误的原因是:
text/jsx
src
script
file://
当您将jsx脚本包含在index.html文件中时,它可以正常工作,因为无需任何请求即可检索jsx脚本。
jsx
您需要做的是在Web服务器上握手,将hello world文件放入该服务器的文档根目录,然后从Web服务器(例如,从URL)加载它们http://localhost/index.html。
http://localhost/index.html