Javascript 从本地文件夹导入type=module的脚本会导致CORS问题
我有一个调用javascript文件的小html文件,但是当我试图在浏览器中访问它时,我得到以下错误: 在以下位置访问脚本: 'file:///C:/Users/jekob/Desktop/battleship/index.js"从源头上说, CORS策略已阻止“null”:跨源请求仅限于 支持协议方案:http、数据、chrome扩展、edge、, https,chrome不受信任 我在谷歌上搜索了几个小时,发现我可以通过服务器(比如node.js)托管我的应用程序,然后允许CORS。但是我不想要任何服务器。我只需要一个简单的html和一个js文件 index.html:Javascript 从本地文件夹导入type=module的脚本会导致CORS问题,javascript,html,cors,es6-modules,Javascript,Html,Cors,Es6 Modules,我有一个调用javascript文件的小html文件,但是当我试图在浏览器中访问它时,我得到以下错误: 在以下位置访问脚本: 'file:///C:/Users/jekob/Desktop/battleship/index.js"从源头上说, CORS策略已阻止“null”:跨源请求仅限于 支持协议方案:http、数据、chrome扩展、edge、, https,chrome不受信任 我在谷歌上搜索了几个小时,发现我可以通过服务器(比如node.js)托管我的应用程序,然后允许CORS。但是我不
<!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>
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)托管我的应用程序
对
然后允许CORS
因为它是同一个来源,所以您不需要CORS
但是我不想要任何服务器。我只需要一个简单的html和一个js文件
那么就不能使用浏览器端ES6模块
您需要首先编写代码以不使用模块,或者使用类似Webpack或Rollup的方法将代码转换为以后不使用模块
我在谷歌上搜索了几个小时,发现我可以通过服务器(比如node.js)托管我的应用程序
对
然后允许CORS
因为它是同一个来源,所以您不需要CORS
但是我不想要任何服务器。我只需要一个简单的html和一个js文件
那么就不能使用浏览器端ES6模块
您需要首先编写代码以不使用模块,或者使用类似Webpack或Rollup的方法将代码转换为以后不使用模块。可以启动Chrome实例(仅针对此网页)使用仅针对此问题的命令行选项
——禁用web安全性并绕过CORs保护。但是,您永远不应该使用此设置进行常规internet浏览,因为它会打开巨大的安全漏洞和漏洞。模块需要服务器。对于小型测试和摆弄,我发现从相关目录启动python HTTP服务器很容易;与python-mhttp.server类似,可以使用命令行选项启动Chrome实例(仅用于此网页),仅用于此问题——禁用web安全性并绕过CORs保护。但是,您永远不应该使用此设置进行常规internet浏览,因为它会打开巨大的安全漏洞和漏洞。模块需要服务器。对于小型测试和摆弄,我发现从相关目录启动python HTTP服务器很容易;类似于python-Mhttp.server
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){
...
};