Skip to content
On this page

CORS ( Cross-Origin Resource Sharing )

CORS 跨域資源公用,是要提供給前端連接使用

py
# 使用Middleware模組
from fastapi.middleware.cors import CORSMiddleware
# 設定開放的網域
origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
]
# 啟動模組設定跨網域連結
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

參考