继续获取 CORS 策略:没有 'Access-Control-Allow-Origin' 即使使用 FastAPI CORSMiddleware

Keep getting CORS policy: No 'Access-Control-Allow-Origin' even with FastAPI CORSMiddleware

我正在开发一个具有快速API 后端和 React 前端的项目。通过 fetch 调用后端时,我有时会得到以下信息:

Access to fetch at 'http://localhost:8000/get-main-query-data' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

这种情况经常发生,我可以调用一个端点然后抛出错误。有时会为所有端点抛出错误

我已经在我的 main.py 中设置了 Middleware,就像这样:(也在这个 line

# allows cross-origin requests from React
origins = [
    "http://localhost",
    "http://localhost:3000",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

这可能是 fetch it's self 的问题吗?我担心当我开始托管时会收到 CORS 错误并且我的原型将无法工作:(

整个main.py是这样的:

后端

""" API to allow for data retrieval and manipulation. """
from typing import Optional

from fastapi import FastAPI, HTTPException, status
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel

import models
from db import Session

app = FastAPI()

""" Pydantic BaseModels for the API. """


class SignalJourneyAudiences(BaseModel):
    """SignalJourneyAudiences BaseModel."""

    audienceId: Optional[int]  # PK
    segment: str
    enabled: bool


class SignalJourneyAudienceConstraints(BaseModel):
    """SignalJourneyAudienceConstraints BaseModel."""

    uid: Optional[int]  # PK
    constraintId: int
    audienceId: int  # FK - SignalJourneyAudiences -> audienceId
    sourceId: int  # FK - SignalJourneySources -> sourceId
    constraintTypeId: int  # FK - SignalJourneyConstraintType -> constraintTypeId
    constraintValue: str
    targeting: bool
    frequency: int
    period: int


class SignalJourneyAudienceConstraintRelations(BaseModel):
    """SignalJourneyAudienceConstraintRelations BaseModel."""

    uid: Optional[int]  # PK
    audienceId: int
    relation: str
    constraintIds: str


class SignalJourneyConstraintType(BaseModel):
    """SignalJourneyConstraintType BaseModel."""

    constraintTypeId: Optional[int]  # PK
    constraintType: str


class SingalJourneySources(BaseModel):
    """SignalJourneySources BaseModel."""

    sourceId: Optional[int]  # PK
    source: str


# allows cross-origin requests from React
origins = [
    "http://localhost",
    "http://localhost:3000",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# database instance
db = Session()


@app.get("/")
def index():
    """Root endpoint."""
    return {
        "messagee": "Welcome to Signal Journey API. Please use the API documentation to learn more."
    }


@app.get("/audiences", status_code=status.HTTP_200_OK)
def get_audiences():
    """Get all audience data from the database."""
    return db.query(models.SignalJourneyAudiences).all()


@app.get("/audience-constraints", status_code=status.HTTP_200_OK)
def get_audience_constraints():
    """Get all audience constraint data from the database."""
    return db.query(models.SignalJourneyAudienceConstraints).all()


@app.get("/audience-constraints-relations", status_code=status.HTTP_200_OK)
def get_audience_constraints_relations():
    """Get all audience constraint data from the database."""
    return db.query(models.SignalJourneyAudienceConstraintRelations).all()


@app.get("/get-constraint-types", status_code=status.HTTP_200_OK)
def get_constraints_type():
    """Get all audience constraint data from the database."""

    return db.query(models.SignalJourneyConstraintType).all()


@app.post("/add-constraint-type", status_code=status.HTTP_200_OK)
def add_constraint_type(sjct: SignalJourneyConstraintType):
    """Add a constraint type to the database."""

    constraint_type_query = (
        db.query(models.SignalJourneyConstraintType)
        .filter(
            models.SignalJourneyConstraintType.constraintType
            == sjct.constraintType.upper()
            and models.SignalJourneyConstraintType.constraintTypeId
            == sjct.constraintTypeId
        )
        .first()
    )

    if constraint_type_query is not None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Constaint type already exists.",
        )

    constraint_type = models.SignalJourneyConstraintType(
        constraintType=sjct.constraintType.upper(),
    )

    db.add(constraint_type)
    db.commit()

    return {
        "message": f"Constraint type {sjct.constraintType.upper()} added successfully."
    }


@app.get("/get-sources", status_code=status.HTTP_200_OK)
def get_sources():
    """Get all sources data from the database."""
    return db.query(models.SingalJourneySources).all()


@app.post("/add-source", status_code=status.HTTP_200_OK)
def add_source_type(sjs: SingalJourneySources):
    """Add a new source type to the database."""
    source_type_query = (
        db.query(models.SingalJourneySources)
        .filter(models.SingalJourneySources.source == sjs.source.upper())
        .first()
    )

    if source_type_query is not None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Source already exists.",
        )

    source_type = models.SingalJourneySources(source=sjs.source.upper())

    db.add(source_type)
    db.commit()

    return {"message": f"Source {sjs.source.upper()} added successfully."}


"""
Endpoints for populating the UI with data. These need to consist of some joins.

Query to be used in SQL 

SELECT
    constraintid,
    sja.segment,
    sjs.source,
    sjct.constrainttype,
    constraintvalue,
    targeting,
    frequency,
    period
FROM signaljourneyaudienceconstraints
JOIN signaljourneyaudiences sja ON sja.audienceid = signaljourneyaudienceconstraints.audienceid;
JOIN signaljourneysources sjs ON sjs.sourceid = signaljourneyaudienceconstraints.sourceid
JOIN signaljourneyconstrainttype sjct ON sjct.constrainttypeid = signaljourneyaudienceconstraints.constrainttypeid
"""


@app.get("/get-main-query-data", status_code=status.HTTP_200_OK)
def get_main_query_data():
    """Returns data for the main query."""
    return (
        db.query(
            models.SignalJourneyAudienceConstraints.constraintId,
            models.SignalJourneyAudiences.segment,
            models.SingalJourneySources.source,
            models.SignalJourneyConstraintType.constraintType,
            models.SignalJourneyAudienceConstraints.constraintValue,
            models.SignalJourneyAudienceConstraints.targeting,
            models.SignalJourneyAudienceConstraints.frequency,
            models.SignalJourneyAudienceConstraints.period,
        )
        .join(
            models.SignalJourneyAudiences,
            models.SignalJourneyAudiences.audienceId
            == models.SignalJourneyAudienceConstraints.audienceId,
        )
        .join(
            models.SingalJourneySources,
            models.SingalJourneySources.sourceId
            == models.SignalJourneyAudienceConstraints.sourceId,
        )
        .join(
            models.SignalJourneyConstraintType,
            models.SignalJourneyConstraintType.constraintTypeId
            == models.SignalJourneyAudienceConstraints.constraintTypeId,
        )
        .all()
    )

前端

我这样调用我的 API 端点:

//form.jsx

 // pulls segments name from signaljourneyaudiences
  useEffect(() => {
    fetch('http://localhost:8000/audiences')
      .then((res) => res.json())
      .then((data) => setSegmentNames(data))
      .catch((err) => console.log(err));
  }, []);

  // pulls field names from signaljourneyaudiences
  useEffect(() => {
    fetch('http://localhost:8000/get-constraint-types')
      .then((res) => res.json())
      .then((data) => setConstraints(data))
      .catch((err) => console.log(err));
  }, []);

// table.jsx


  useEffect(() => {
    fetch('http://localhost:8000/get-main-query-data')
      .then((res) => res.json())
      .then((data) => {
        setTableData(data);
      })
      .catch((err) => console.log(err));
  }, []);

正如您在此处看到的那样,table 已由端点填充,但另一方面,其中一个下拉列表没有。

HTTP 500 错误描述

INFO:     127.0.0.1:62301 - "GET /get-constraint-types HTTP/1.1" 500 Internal Server Error
2022-02-24 09:26:44,234 INFO sqlalchemy.engine.Engine [cached since 2972s ago] ()
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1702, in _execute_context
    context = constructor(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 1013, in _init_compiled
    self.cursor = self.create_cursor()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 1361, in create_cursor
    return self.create_default_cursor()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 1364, in create_default_cursor
    return self._dbapi_connection.cursor()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 1083, in cursor
    return self.dbapi_connection.cursor(*args, **kwargs)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 6191820800 and this is thread id 6174994432.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/uvicorn/protocols/http/httptools_impl.py", line 372, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/fastapi/applications.py", line 259, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/middleware/cors.py", line 92, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/middleware/cors.py", line 147, in simple_response
    await self.app(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/routing.py", line 61, in app
    response = await func(request)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/fastapi/routing.py", line 227, in app
    raw_response = await run_endpoint_function(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/fastapi/routing.py", line 162, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/starlette/concurrency.py", line 39, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/anyio/to_thread.py", line 28, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
    return await future
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 754, in run
    result = context.run(func, *args)
  File "/Users/paul/Developer/signal_journey/backend/./main.py", line 109, in get_constraints_type
    return db.query(models.SignalJourneyConstraintType).all()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2759, in all
    return self._iter().all()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2894, in _iter
    result = self.session.execute(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1692, in execute
    result = conn._execute_20(statement, params or {}, execution_options)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1614, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1481, in _execute_clauseelement
    ret = self._execute_context(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1708, in _execute_context
    self._handle_dbapi_exception(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2026, in _handle_dbapi_exception
    util.raise_(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1702, in _execute_context
    context = constructor(
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 1013, in _init_compiled
    self.cursor = self.create_cursor()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 1361, in create_cursor
    return self.create_default_cursor()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 1364, in create_default_cursor
    return self._dbapi_connection.cursor()
  File "/Users/paul/.local/share/virtualenvs/backend-CF5omcRU/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 1083, in cursor
    return self.dbapi_connection.cursor(*args, **kwargs)
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 6191820800 and this is thread id 6174994432.
[SQL: SELECT "SignalJourneyConstraintType"."constraintTypeId" AS "SignalJourneyConstraintType_constraintTypeId", "SignalJourneyConstraintType"."constraintType" AS "SignalJourneyConstraintType_constraintType" 
FROM "SignalJourneyConstraintType"]
[parameters: [{}]]
(Background on this error at: https://sqlalche.me/e/14/f405)

当发生服务器端错误(响应代码 5xx)时,CORS 中间件不会添加它们的 headers,因为请求已有效终止,使浏览器无法读取响应。

对于第二个问题,您应该为 API 的每次调用使用单独的 session。 The reference guide has an example of how to do this:

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

...

# Dependency
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

...

@app.post("/users/{user_id}/items/", response_model=schemas.Item)
def create_item_for_user(..., db: Session = Depends(get_db)):
    return crud.create_user_item(db=db, item=item, user_id=user_id)