且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决“对预检请求的响应未通过访问控制检查:它没有HTTP正常状态”使用Node.js API的React App中出现错误

更新时间:2023-02-20 19:00:14

由于您使用的是 webpack-dev-server ,您可以使用 proxy 选项 DevServerProxy

Since you're using webpack-dev-server you can use the proxy option DevServerProxy.

您的配置将如下所示:

// webpack.config.js
devServer: {
    proxy: {
      '/internal': 'http://localhost:1337'
    }
  }

因为我看不到您的 express 关于您的问题的路由如果您的 API 生活在 / internal上,我正在猜测代理路由端点,那么您应该像这样修改React代码:

Since I can't see your express routes on your question I'm speculating about the proxy route if your API lives on /internal endpoint then you should modify your React code like this:

const response = await fetch('/internal/provider/check_email_exist', {
   method: 'POST',
   headers,
   body,
})

如您所见,我省略了 https:// localhost:1337 导致 webpack-dev-server 中的 proxy 选项将处理此问题,并将重定向到 http:// localhost:1337 。希望这会帮助你。欢呼,笑起来。

As you can see I ommited the https://localhost:1337 because the proxy option from webpack-dev-server will handle this and it will redirect to http://localhost:1337. Hope this will help you. Cheers, sigfried.

编辑

对您的问题的评论指出您应该在 express 服务器而不是客户端上设置标头,对于此任务,您可以使用 cors-middleware 包。

As the comment on your question pointed out you should set the headers on your express server, not the client, for this task you can use the cors-middleware package.