How can you use my Api made with FastAPI, from my localhost, from an external html, for example, it is my simple implementation of test:
main.py:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def main():
return {"message": "Hello World"}
index.html:
<html>
<head>
<title>Item Details</title>
</head>
<body>
<script>
//var url = 'http://localhost:8000';
fetch('http://127.0.0.1:8000/')
.then(res => res.json())
.then(data => {console.log(data)})
</script>
<h1></h1>
</body>
</html>
but the return navigator(Safari) is:
[Error] Origin null is not allowed by Access-Control-Allow-Origin. [Error] Fetch API cannot load http://127.0.0.1:8000/ due to access control checks. [Error] Failed to load resource: Origin null is not allowed by Access-Control-Allow-Origin. (127.0.0.1, line 0) [Error] Unhandled Promise Rejection: TypeError: Origin null is not allowed by Access-Control-Allow-Origin. (anonymous function) promiseReactionJob
Origin null is not allowed- how are you loading the index.html?http://?file:///?