首页 > 解决方案 > 运行 'react-script start' 得到 400 错误请求

问题描述

我正在开发一个简单的 RoR 后端、React 前端应用程序,并且我有一些 API 请求来从数据库中获取数据。目前,我正在使用 axios 发出如下请求:

axios.get('/api/session/user')

问题是当我bundle exec rails server在终端上运行服务器时,它会发出正确的请求并获取数据。但是,当我运行npm start“react-script start”时,我总是在我提出请求的任何 api 上收到 400 Bad Request。

我使用 Postman 进行了测试,以获取或发布数据都在运行rails servernpm start但只有在使用npm start. 我需要做什么才能使其在react-script start( npm start) 中运行?

在 package.json 中,我将代理设置为"proxy": "http://localhost:3000/". 但是,当我将端口更改为 3001 时,我得到 500 Internal Server Error 而不是 400 Bad Request。当我运行时,端口 3000 和 3001 都可以正常工作rails server

标签: ruby-on-railsaxioshttpclientreact-scripts

解决方案


推荐阅读