【问题标题】:How to async request to a sync API?如何异步请求到同步 API?
【发布时间】:2018-10-09 06:28:14
【问题描述】:

我使用SQLAlchemyFlaskAuthlibOauth 框架)在 Python 中构建了一个 REST API。

此 API 在我已配置 SSL、CORS、永久会话的 Docker 容器上运行...

当我尝试在 React 应用程序中使用 Axios 在 API 上请求令牌时,它可以工作:

    axios({
      method: "post",
      url: `.../user/login`,
      data: {username, password}
    }).then(response => {
      ...
    }).catch(error => {
      ...
    })

但是当我尝试撤销这个向注销中间件端点发出 Axios 请求的令牌时,我收到 Uncaught (in promise) Error: Request failed with status code 403 (img):

  axios({
    method: "post",
    url: `.../user/logout`,
    headers: {Authorization: `Bearer ${this.props.login.access_token}`},
  }).then(response => {
    ...
  }).catch({
    ...
  })

我不明白。如果我使用PostmanInsonia 之类的应用程序发出令牌并撤销,它就可以工作。

我该如何解决?为什么会这样?

我研究并找到了一些线程,但我仍然有这个问题:Async requests to a web serviceUncaught (in promise) Error: Request failed with status code 404 ...

Obs ¹:出于法律原因,我无法向我的雇主展示 API 源代码

Obs ²:我假设这是一个异步/同步问题,我不是 100% 确定

【问题讨论】:

  • 邮递员显示什么?
  • I'm assuming it's a async / sync problem 请放心,这与此无关...服务器理解请求但拒绝满足它 - 请检查浏览器开发人员工具网络选项卡,特别是正在发送的标头并收到 logout 请求 - 并检查由 Authorization 请求标头触发的 OPTIONS 预检请求,网络选项卡中有什么响应?

标签: javascript python reactjs api


【解决方案1】:

通常,当您没有发出请求的权限时,您会收到 Forbidden 403 响应。我认为这与CORS有关。您能否检查浏览器中的“网络”选项卡以确保您(通过 Postman 和浏览器中)发出的请求看起来相同,如果有 OPTIONS 请求,响应是什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 2016-08-19
    • 2023-04-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多