CrossOrigin
前言
前端采用Vue,后端采用fastAPI的CV项目在开发时遇到跨域问题,记录学习过程与解决方案。
概念
- CORS
- Origin
- Preflight Request
解决方法
采用CORS中间件,后端添加allowed origins列表
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
// 配置允许域名
origins = [
"http://localhost.tiangolo.com",
"https://localhost.tiangolo.com",
"http://localhost",
"http://localhost:8080",
]
// 配置允许域名列表、允许方法、请求头、cookie等
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)