目前,如果我手动编码要发送的数据,我有以下功能可以正确地进行 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 中提供这个帖子请求?
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 表单,但它不会做任何事情。
所以我迷路了,因为我不知道如何用表单中的数据来提供请求。