AI掘金头条新闻系统 (Toutiao News)-解决跨域问题
·
1. main.py
# 允许的来源(可以是域名列表)
# origins = [
# "http://localhost",
# "http://localhost:3000",
# "https://your-frontend-domain.com" # 你的服务器IP/域名
# ]
# 添加 CORS 中间件
app.add_middleware(
CORSMiddleware,
# allow_origins=[origins], # 生成环境
allow_origins=["*"], # 允许访问的源
allow_credentials=True, # 允许携带 Cookie
allow_methods=["*"], # 允许所有请求方法
allow_headers=["*"], # 允许所有请求头
)
完整代码
from fastapi import FastAPI
from routers import news
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
# 挂载路由/注册路由
app.include_router(news.router)
# 允许的来源(可以是域名列表)
# origins = [
# "http://localhost",
# "http://localhost:3000",
# "https://your-frontend-domain.com" # 你的服务器IP/域名
# ]
# 添加 CORS 中间件
app.add_middleware(
CORSMiddleware,
# allow_origins=[origins], # 生成环境
allow_origins=["*"], # 允许访问的源
allow_credentials=True, # 允许携带 Cookie
allow_methods=["*"], # 允许所有请求方法
allow_headers=["*"], # 允许所有请求头
)
更多推荐
所有评论(0)