如何解决React和快速API中的CORS错误

发布于 2025-02-04 18:12:19 字数 1731 浏览 3 评论 0 原文

我在快速API中添加了laster_origins,但是错误仍然是SAEM,请提供帮助,

这是我尝试连接并发出消息的React代码:

import io from 'socket.io-client'

const socket = io(
  'https://samsara-chat.herokuapp.com/chatroom/check-chat?token=8acfbead5bb2070f112975dc188400b4bac8127b&receiver=saken2.0'
)

function Socket() {
  const [message, setMessage] = useState()
  console.log(message, 'message')
  const sendMessage = () => {
    socket.emit('message:', { message: message })
  }
  return (
    <>
      <input type="text" onChange={(e) => setMessage(e.target.value)} />
      <button onClick={sendMessage}>send message</button>
    </>
  )
}

export default Socket

这是我给出的快速API代码,我给出了允许Origins

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from server.routers import chatrooms
from server.routers.socketio import sio

socket_app = socketio.ASGIApp(sio)

# TODO CORS
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    # allow_origins=['*'],
    allow_origins=["http://localhost","http://127.0.0.1:3000", 'http://localhost:8080', "http://localhost:3000", "https://samsara-web.netlify.app"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
app.include_router(chatrooms.router, prefix='/chatroom')

app.mount("/", socket_app)

问题是问题。在'https://samsara-chat.herokuapp.com/socket.io/?token=6BA855C85C88F67C3FBF9A864EF617E41A696745FB&amp.amp = saken2.saken2.0&emp.eio = amantport中/本地主机:3000'已被CORS策略阻止:'access-control-allow-oferigin'标头包含多个值'http:// localhost:3000,http:// http:// localhost:3000',但只允许使用一个。让服务器以有效的值发送标题,或者,如果不透明响应满足您的需求,请将请求模式设置为“ no-cors”,以通过禁用CORS来获取资源。

I added allow_origins in fast api, but the error remains the saem , please help,

here is my react code where I try to connect and emit the message:

import io from 'socket.io-client'

const socket = io(
  'https://samsara-chat.herokuapp.com/chatroom/check-chat?token=8acfbead5bb2070f112975dc188400b4bac8127b&receiver=saken2.0'
)

function Socket() {
  const [message, setMessage] = useState()
  console.log(message, 'message')
  const sendMessage = () => {
    socket.emit('message:', { message: message })
  }
  return (
    <>
      <input type="text" onChange={(e) => setMessage(e.target.value)} />
      <button onClick={sendMessage}>send message</button>
    </>
  )
}

export default Socket

Here is my fast api code where I gave allow origins

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from server.routers import chatrooms
from server.routers.socketio import sio

socket_app = socketio.ASGIApp(sio)

# TODO CORS
app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    # allow_origins=['*'],
    allow_origins=["http://localhost","http://127.0.0.1:3000", 'http://localhost:8080', "http://localhost:3000", "https://samsara-web.netlify.app"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
app.include_router(chatrooms.router, prefix='/chatroom')

app.mount("/", socket_app)

The problem is Access to fetch at 'https://samsara-chat.herokuapp.com/socket.io/?token=6ba855c88f67c3fbf9a864ef617e41a696745fbb&receiver=saken2.0&EIO=4&transport=polling&t=O4uyi_R' from origin 'http://localhost:3000' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://localhost:3000, http://localhost:3000', but only one is allowed. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

在巴黎塔顶看东京樱花 2025-02-11 18:12:20

这应该有帮助:

origins = [
    "*"
]

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

This should help:

origins = [
    "*"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文