首页 > 解决方案 > 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,但仍然没有。

标签: reactjsasp.net-web-apiasp.net-coreaxios

解决方案


您需要使用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 文档这个问题中找到


推荐阅读