【问题标题】:React Native and FastAPI issueReact Native 和 FastAPI 问题
【发布时间】:2020-09-19 20:48:30
【问题描述】:

我在创建 React Native 应用程序时遇到了问题。我正在使用 FastAPI 作为后端。对我的后端进行此调用时,没有任何反应。

  function sendAkt() {
    fetch("https://192.168.1.5:8000/newActivity", {
      method: "POST",
      headers: {
        'Accept': "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        name: ime,
        desc: opis,
        place: mjesto,
        team: tim,
        typeOf: pickerSel,
        id: users.id,
      }),
    })
    .then(_=> console.log('rea for the minute'))
    .catch(e => console.log(e))
  }

没有错误,后端的控制台从来没有报告过对后端路由的调用。似乎提取只是挂起。因为大约 3 分钟后,这会在控制台中弹出

Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:505:17 in setTimeout$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:387:16 in callTimers
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

这是我调用的后端路由

@app.post('/newActivity')
async def unos(request: NewActivity):
    print('route Reaches')
    # name = request.name
    # desc = request.desc
    # place = request.place
    # team = request.team
    # typeOf = request.typeOf
    # id = request.id
    # year = 10

    # new_activity = Activities(activity_type_id=typeOf, academic_year_id=year,
    #                           description=desc,
    #                           user_id=id, team_id=team, title=name, location=place)

    # session.add(new_activity)
    # session.commit()

    return JSONResponse(content='Uspijeh')

这是我后端的 CORS 设置

app.add_middleware(
    CORSMiddleware,
    allow_origins=['http://localhost:19001'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

我已经在这里工作了好几个小时,但我不知道是什么导致了这个问题

【问题讨论】:

  • 您是否 100% 确定您的服务器可以通过 192.168.1.5:8000 访问?
  • 您使用 Uvicorn 还是其他服务器?你如何启动你的服务器?
  • 是的,因为我使用相同的应用程序通过不同的路由到达服务器。我正在使用 Uvicorn(从命令 Uvicorn main:app --reload --host 0.0.0.0 开始)
  • 你使用的是 docker 还是一些容器?
  • 不能在本地运行

标签: javascript python reactjs react-native fastapi


【解决方案1】:

真是愚蠢的问题。我用https 调用本地服务器而不是http. Changing to http` 修复了它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多