我想从我的React JS FrontEnd向我的Backend ASP.NET Core Web API发出发布请求,但是使用@ axios @时不会触发该请求。
我按照互联网上有关发出axios获取/发布请求的说明进行操作,但似乎不起作用
handleRegistration = (event) => {
event.preventDefault();
const user = {
email: this.state.email,
firstName: this.state.firstName,
lastName: this.state.lastName,
password: this.state.password
};
axios.post('http://localhost:{somenumbershere}/api/values', { user })
.then(res => {
console.log(res);
console.log(res.data);
})
}
我正在创建一个用户,并希望将其作为对象发送给我的Action方法到后端,但它从未遇到断点。如您所见,我将obj发送到的URL,@ api / values / 5 @是发送到后端actiom方法的示例路由。我已经用HttpPost装饰了Action,但是仍然没有。
答案 0 :(得分:0)
您需要使用FormData
对象发送数据,如下所示:
var formData = new FormData();
formData.append('email', this.state.email);
formData.append('firstName', this.state.firstName);
formData.append('password', this.state.password);
formData.append('password', this.state.password);
更多详细信息,请参见FormData doc和in this question
答案 1 :(得分:0)
删除将用户包装在axios.post(...,user)中的{}