2016-08-03 46 views
0

我是React學習的新手。我剛剛從這個網站上下載了Starter Kit 15.3.0:https://facebook.github.io/react/docs/getting-started.html在chrome中加載react.js的錯誤

我正在使用chrome瀏覽器。當試圖包括單獨的js文件轉換成HTML頁面一樣
https://facebook.github.io/react/docs/getting-started.html#separate-file
說,鉻報告錯誤:

XMLHttpRequest cannot load file:///C:/Users/jxu200/Desktop/react-15.3.0/src/helloworld.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. 

我注意到,它是由提到反應團隊:

Note that some browsers (Chrome, e.g.) will fail to load the file unless it's served 
via HTTP. 

我的問題是:爲什麼

react.js/react-dom.js/react-dom-server.js/react-with-addons.js, 

所有js文件在/ build文件夾中c一個沒有問題的加載,但由我自己創建的js文件不能正常加載?即使我把我的js文件放在/ build文件夾中?加載react.js代碼時,chrome實際上會做什麼?

+0

W沒有看到你如何加載該文件,這是不可能回答這個問題。 – zerkms

+2

作爲腳本標籤加載的文件不容易受到相同的源策略影響(請閱讀它的內容)。您應該使用靜態HTTP開發服務器 –

回答

0

您需要一些開發服務器,如註釋中所指出的那樣。您可以將webpack用於較大的項目,但僅用於運行示例,您應該可以使用簡單的serve npm包進行處理。

  1. 安裝​​
  2. 打開節點命令提示(或控制檯如果你使用Linux/OSX)
  3. 安裝NPM servenpm install -g serve
  4. 從命令行導航到您的起動器工具包文件夾,並運行serve命令
  5. 在瀏覽器中導航到http://localhost:3000/
+0

那麼爲什麼入門工具包15.3.0中的所有示例都不需要服務器?入門套件15.3.0源代碼來自https://facebook.github.io/react/docs/getting-started.html#starter-pack –

+0

正如您已經指出反應團隊的注意事項,如果您直接打開文件那麼你正在使用文件協議,並且一些瀏覽器不會以這種方式加載腳本。因此你需要HTTP服務器。 http://stackoverflow.com/questions/23536450/cant-open-angular-app-using-file-protocol-in-chrome – Majky

+0

明白了你的觀點!非常感謝您的解釋! –