• <bdo id='Th3kh'></bdo><ul id='Th3kh'></ul>
  • <legend id='Th3kh'><style id='Th3kh'><dir id='Th3kh'><q id='Th3kh'></q></dir></style></legend><tfoot id='Th3kh'></tfoot>
  • <small id='Th3kh'></small><noframes id='Th3kh'>

  • <i id='Th3kh'><tr id='Th3kh'><dt id='Th3kh'><q id='Th3kh'><span id='Th3kh'><b id='Th3kh'><form id='Th3kh'><ins id='Th3kh'></ins><ul id='Th3kh'></ul><sub id='Th3kh'></sub></form><legend id='Th3kh'></legend><bdo id='Th3kh'><pre id='Th3kh'><center id='Th3kh'></center></pre></bdo></b><th id='Th3kh'></th></span></q></dt></tr></i><div id='Th3kh'><tfoot id='Th3kh'></tfoot><dl id='Th3kh'><fieldset id='Th3kh'></fieldset></dl></div>

      1. 从本地文件夹导入 type=module 的脚本会导致 CORS 问题

        时间:2023-05-16

        <small id='3YczT'></small><noframes id='3YczT'>

          <bdo id='3YczT'></bdo><ul id='3YczT'></ul>

          <i id='3YczT'><tr id='3YczT'><dt id='3YczT'><q id='3YczT'><span id='3YczT'><b id='3YczT'><form id='3YczT'><ins id='3YczT'></ins><ul id='3YczT'></ul><sub id='3YczT'></sub></form><legend id='3YczT'></legend><bdo id='3YczT'><pre id='3YczT'><center id='3YczT'></center></pre></bdo></b><th id='3YczT'></th></span></q></dt></tr></i><div id='3YczT'><tfoot id='3YczT'></tfoot><dl id='3YczT'><fieldset id='3YczT'></fieldset></dl></div>
        • <tfoot id='3YczT'></tfoot>

              <tbody id='3YczT'></tbody>
            <legend id='3YczT'><style id='3YczT'><dir id='3YczT'><q id='3YczT'></q></dir></style></legend>

                  本文介绍了从本地文件夹导入 type=module 的脚本会导致 CORS 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个调用 javascript 文件的小 html 文件,但是当我尝试在浏览器中访问它时,出现以下错误:

                  I have a little html file that calls a javascript file, but when I'm trying to access it in the browser I'm getting the following error:

                  在以下位置访问脚本'file:///C:/Users/jekob/Desktop/battleship/index.js' 来自原产地null"已被 CORS 策略阻止:跨源请求仅支持协议方案:http、data、chrome-extension、edge、https,chrome 不受信任.

                  Access to script at 'file:///C:/Users/jekob/Desktop/battleship/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.

                  我已经用谷歌搜索了几个小时,发现我可以通过服务器(如 node.js)托管我的应用程序,然后允许 CORS.但是我不想要任何服务器.我只想要一个简单的 html 和一个 js 文件.

                  I've googled that for hours and found out that I can host my app by a server (like node.js) and then to allow CORS. However I don't want any server. I want just a simple html and a js file.

                  index.html:

                  index.html:

                  <!DOCTYPE html>
                  <html>
                  <head>
                      <title>battle-ship</title>
                      <link rel="stylesheet" type="text/css" href="index.css">
                      
                  </head>
                  <body>
                  <div id="board"></div>
                  <script type="module"  src="index.js"></script>
                  </body>
                  </html>
                  

                  index.js:

                  import {board} from './board_0.1.js';
                  
                  console.log(board);
                  

                  board.js:

                  class cell{
                      constructor(){
                          this.locationByLetter = null;
                          this.locationByNumb = [];
                          this.occupied = false;
                          this.clicked = false;
                      }
                  }
                  
                  class shipDitel{
                      constructor(name,size){
                          this.name = name;
                          this.size = size;
                          this.location =[];
                      }
                  }
                  
                  export const board = buildBoard();
                  const shipType = [["Destroyer",2/*size*/],["Submarine",3],["Cruiser",3,],
                                    ["Battleship",4,],["AircraftCarrier",5]];
                  
                  var shipsArr=setShip();
                  var selectedShip =  selectShip(shipsArr[4]);
                  var stateGame ={
                      setting:true,
                      gameIsStart:false
                      }
                  
                  function buildBoard(){
                  ..
                  };
                  
                  function setShip(){   //setting to a ship  his name and size .
                  ...
                  };
                  
                  function selectShip(ship){
                      ...
                  }
                  
                  function  onSelectedCell(cell){
                      ...
                  };    
                  
                  function checkTheZone(cell){  
                  ...
                  };
                  

                  推荐答案

                  我已经用谷歌搜索了几个小时,发现我可以通过服务器(如 node.js)托管我的应用程序

                  I've googled that for hours and found out that I can host my app by a server (like node.js)

                  是的

                  然后允许 CORS.

                  由于它将是同源,因此您不需要 CORS.

                  Since it will be Same Origin, you won't need CORS.

                  但是我不想要任何服务器.我只想要一个简单的 html 和一个 js 文件.

                  However I don't want any server. I want just a simple html and a js file.

                  那你就不能使用浏览器端的 ES6 模块了.

                  Then you can't use browser-side ES6 modules.

                  您需要先编写代码以不使用模块,或者使用 Webpack 或 Rollup 之类的东西将代码转换为以后不使用模块.

                  You'll need to either write the code to not use modules in the first place, or use something like Webpack or Rollup to convert the code to not use modules afterwards.

                  这篇关于从本地文件夹导入 type=module 的脚本会导致 CORS 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:HTTP 预检 (OPTIONS) 请求仅在 IE 中失败 下一篇:BigCommerce API 是否支持 CORS?

                  相关文章

                  <tfoot id='EjrUr'></tfoot>

                  1. <legend id='EjrUr'><style id='EjrUr'><dir id='EjrUr'><q id='EjrUr'></q></dir></style></legend>
                    <i id='EjrUr'><tr id='EjrUr'><dt id='EjrUr'><q id='EjrUr'><span id='EjrUr'><b id='EjrUr'><form id='EjrUr'><ins id='EjrUr'></ins><ul id='EjrUr'></ul><sub id='EjrUr'></sub></form><legend id='EjrUr'></legend><bdo id='EjrUr'><pre id='EjrUr'><center id='EjrUr'></center></pre></bdo></b><th id='EjrUr'></th></span></q></dt></tr></i><div id='EjrUr'><tfoot id='EjrUr'></tfoot><dl id='EjrUr'><fieldset id='EjrUr'></fieldset></dl></div>

                      <bdo id='EjrUr'></bdo><ul id='EjrUr'></ul>

                      <small id='EjrUr'></small><noframes id='EjrUr'>