【发布时间】:2020-04-18 02:33:29
【问题描述】:
目前,如果我手动编码要发送的数据,我有以下功能可以正确地进行 POST
constructor() {
super()
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const form = new FormData(event.target);
axios({
method: 'post',
url: 'http://localhost:3003/register',
data: qs.stringify({
email: 'testing', //as you can see I have handcoded the data, and it posts properly
password: 'testing'
}),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
})
}
render() {
return (
<div className="content-landing">
<form onSubmit={this.handleSubmit}>
<label htmlFor="email">Enter email</label>
<input id="email" name="email" type="email" />
<label htmlFor="password">Enter password</label>
<input id="password" name="password" type="password" />
<button>Register</button>
</form>
</div>
)
}
}
但是我如何在 axios 中将表单中的数据提供给这个 post 请求?
axios({
method: 'post',
url: 'http://localhost:3003/register',
data: qs.stringify({
form //referencing const form
}),
headers: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
})
我已经尝试使用上面的代码来获取带有表单数据的 const 表单,但它不会做任何事情。
所以我迷路了,因为我不知道如何用表单中的数据来提供请求。
【问题讨论】:
-
我已经为您提供了完整的解决方案。检查一下,如果有帮助,请告诉我:)