简体   繁体   中英

Cross origin requests on local

I'm trying something really simple but for some reason it doesn't work :

index.html :

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>SyriLab</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
    <header></header>
    <div id="content"></div>

    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/poper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/functions.js"></script>
    <script src="js/main.js"></script>
</body>
</html>

js/main.js :

window.onload=function(){
    main();
}


function main(){
    $("header").load("./pages/header.html"); 
    $("#content").load("./pages/home.html");
}

Errors I get when I launch index.html :

Failed to load file:///E:/Dev/Eclipse/SyriLab/pages/header.html: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Failed to load file:///E:/Dev/Eclipse/SyriLab/pages/home.html: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Everything is local, same root, I'm just trying to make a basic html page, including bootstrap and jquery (poper too, not sure what it is but was on the bootstrap page). And using something similar to "include" in php, but with regular js and html.

What am I doing wrong here ?

Based on your question, it seems you are trying to access the index.html as a local file. Instead of that, you must use webserver (eg nginx, apache etc) to access the file. The jQuery's load method will not be able to load the file due to the protocol used for accessing local file is file:// . Such requests are prohibited by the browsers due to security reasons.

Configure a webserver and try to access the index.html using http protocol and your code should work.

As others has stated. You must serve your files using a server.

For that purpose and to avoid software installation use Python.

In your console type:

cd /path/to/my/index.html
python -m http.server

By default it will use http://0.0.0.0:8000/

This simple server will serve your directory files. Index.html is the empty resource request that the server will try to find and serve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM