首页 > 解决方案 > 将 axios 发布请求反应到 wcf Web 服务

问题描述

我正在尝试将数据从反应应用程序发布到 wcf 服务类。它是一个简单的对象,只有名称年龄和性别。正在从反应应用程序调用该服务,但没有数据。如果我使用获取请求和参数传递数据正在传递给服务,但我想使用发布请求。是否需要匹配 wcf 服务类中的字段名称。

为了在反应组件中进行测试,我正在使用此代码

const headers = {
            'content-type': 'application/json'
          }

    const puser = {
      Name:'Test',
      Age:'23',
      Gender:'F',
    }
    axios.post('http://localhost:3292/myService.svc/adduser',{headers:headers}, puser)
    .then(res=> {
      console.log(res.data)
    })

在 wcf 服务中,类代码是

public class UserClass
    {

        string name;
        int age;
        string gender;

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public int Age
        {
            get
            {
                return age;
            }

            set
            {
                age = value;
            }
        }

        public string Gender
        {
            get
            {
                return gender;
            }

            set
            {
                gender = value;
            }
        }
}

并在服务电话中

public string AddUser(List<User> U)
        {
            UserClass usr = new UserClass();
        return "success";
    }

标签: reactjsapiclassobjectwcf

解决方案


Axios post 方法需要data作为第二个参数。尝试puser{headers}. https://github.com/axios/axios#axiosposturl-data-config


推荐阅读