- FastAPI 教程
- FastAPI - 首頁
- FastAPI - 介紹
- FastAPI - 歡迎介面
- FastAPI -OpenAPI
- FastAPI - Uvicorn
- FastAPI - 型別提示
- FastAPI - IDE 支援
- FastAPI - Rest 架構
- FastAPI - 路徑引數
- FastAPI - 查詢引數
- FastAPI - 引數驗證
- FastAPI - Pydantic
- FastAPI - 請求體
- FastAPI - 模板
- FastAPI - 靜態檔案
- FastAPI - HTML 表單模板
- FastAPI - 訪問表單資料
- FastAPI - 上傳檔案
- FastAPI - Cookie 引數
- FastAPI - Header 引數
- FastAPI - 響應模型
- FastAPI - 巢狀模型
- FastAPI - 依賴項
- FastAPI - CORS
- FastAPI - Crud 操作
- FastAPI - SQL 資料庫
- FastAPI - 使用 MongoDB
- FastAPI - 使用 GraphQL
- FastAPI - Websockets
- FastAPI - FastAPI 事件處理程式
- FastAPI - 安裝子應用
- FastAPI - 中介軟體
- FastAPI - 安裝 Flast 應用
- FastAPI - 部署
- FastAPI 有用資源
- FastAPI - 快速指南
- FastAPI - 有用資源
- FastAPI - 討論
FastAPI - CORS
跨域資源共享 (CORS) 是當一個執行在某個客戶端瀏覽器上的前端應用程式試圖透過 JavaScript 程式碼與後端進行通訊的情況,而該後端與前端的“原點”是不同的。此處原點是協議、域名和埠號的組合。因此,https:// 和 https:// 有不同的原點。
如果一個 URL 的瀏覽器傳送請求要求執行來自另一個原點的 JavaScript 程式碼,則該瀏覽器會發送一個 OPTIONS HTTP 請求。
如果後端透過傳送適當的頭部來自該不同原點的授權通訊,它將允許前端 JavaScript 傳送其請求到後端。為此,後端必須具有“允許原點”的列表。
要顯式指定允許的原點,請匯入 CORSMiddleware 並將原點列表新增到應用中介軟體。
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = [
"http://192.168.211.:8000",
"https://",
"https://:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def main():
return {"message": "Hello World"}
廣告