7

I'm trying to learn react.js, but got stuck on "Hello World" script.

My 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>

and src/helloworld.js:

React.render(
  <h1>Hello, world!</h1>,
  document.getElementById('example')
);

When I put this code inside <script> in index.html file it works fine, but when I move it to seperate file I get blank page, and console error:

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.

What is wrong with it?

0

3 Answers 3

9

You get that error because:

  1. You have loaded the index.html from your local file system (e.g. by double clicking on it), instead of loading it via a web server
  2. The JSX transformer, the one responsible of text/jsx scripts is a javascript component that tries to fetch the file specified by the src attribute of the script tag
  3. Javascript is allowed to fetch external resources only from the protocols enumerated in the error message (and even for those has further limitation like cross-domain requess); files loaded from the local file system have the file:// protocol which is not within that list.

When you included the jsx script in the index.html file it worked as no requests were needed in order to retrieve the jsx script.

What you need to do is grab your hands on a web server, place the hello world files into the document root of that server, and load them from the web server, e.g. from an URL like http://localhost/index.html.

Sign up to request clarification or add additional context in comments.

2 Comments

is there any way to solve this without needing a server? I see many answers saying you need a server but the questions are usually about being able to run the js code in the browser. if I wanted, for instance, to include the result of a project created with create-react-app as a static page in another project, not a whole app
@raquelhortab if you can get past issue #3 from my answer, then you might be able to get away without a server
8

I suggest you boot up a web server.

This python -m SimpleHTTPServer will boot up a simple web server.

You can run this in your directory. Access it here http://localhost:8000.

Or, you can use Chrome flags and add this line --allow-file-access-from-files

Notes

  1. Python come pre-packaged with an OSX installation, so if you're on a Mac, good.

  2. Chrome flags is not recommended as it is tedious of a process.

1 Comment

Python 3 syntaxis would be helpful as well: python3 -m http.server [PORT]
1

I know the answer is already approved but for reference I post code here. We can use react.js with script task like this

<!DOCTYPE html>
<html>
  <head>
   <script crossorigin src="https://unpkg.com/react@15/dist/react.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
<script crossorigin src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
    ReactDOM.render(
      <h1>Hello, world!</h1>,
      document.getElementById('example')
    );
</script>
  </head>
  <body>
    <div id="example"></div>
  </body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.