reactjs - Axios Post Method 没有激活BackEnd中的Action
问题描述
我想从我的 React JS 前端向我的后端 ASP.NET Core Web API 发出一个发布请求,但是在使用 @axios@ 时它不会被触发。
我按照互联网上的一些说明进行了 axios get/post 请求,但它似乎不起作用
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 方法到 BackEnd,但它从未达到断点。如您所见,我将 obj 发送到的 url,@api/values/5@ 是发送到后端 action 方法的示例路由。我已经用 HttpPost 装饰了 Action,但仍然没有。
解决方案
您需要使用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 文档和这个问题中找到
推荐阅读
- firebase - Gmail API - 从 Firebase 存储的 URL 发送附件
- php - 仅选择与 Laravel 相关的行
- windows - 在 Windows 注册表中创建根密钥
- sql - 当我知道记录是唯一的时查询抱怨唯一性违规
- php - 在php中根据它们的键值合并数组
- ios - 如何断开已经连接的BLE设备
- google-cloud-platform - Vision API 的话题性和分数始终相同
- vue.js - Nuxt 在生成脚本期间挂起
- c++ - 用于解耦 API 使用的回调
- python - Python, cx_Freeze "python setup.py build" 没有做任何事情