【发布时间】:2019-02-19 18:37:08
【问题描述】:
我在 React 应用程序中使用 Axios 来访问 api。我正在尝试将登录详细信息发布到服务器,但是当我记录请求正文时,我得到的只是{}。
这是请求:
handleSubmit() {
let email = this.state.email
let password = this.state.password
request.post("/login",{email: email, password: password}) //just prints {} on the backend
.then((results)=>console.log(results))
.catch((err)=>console.log(err))
}
如果我删除大括号并发送一个类似hello 的字符串作为测试,我会返回{ hello: '' },很明显它的工作原理。在这个阶段,我已经尝试了所有方法来获取用户在该字段中的输入,但没有成功。
这是服务器代码:
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({
extended: true
}));
app.post("/login",(req,res) => {
console.log(req.body);
})
我的 api 是使用 express.js 在端口 4000 上的节点服务器
【问题讨论】:
-
你的服务器代码是什么样的?你的快递服务器有 JSON 正文解析器中间件吗?
-
是的,我正在使用body-parser,在使用
<form method="post" action="localhost:4000/login">.....</form>之前它都成功了
标签: javascript node.js express axios