ASP.NET后端服务器带参Post请求415和400问题

使用core api编写接口服务器时,写了如下接口

使用swagger UI 和 Postman调用都不会有问题

但使用vue、jquery发起ajax的post 或者 HttpWebRequest调用就会出现415或者400

然后这是我的web.config的配置

  <!--允许跨域-->
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
        <!--Access-Control-Allow-Credentials: true
        Access-Control-Max-Age: 1728000-->
      </customHeaders>
    </httpProtocol>
  </system.webServer>

方法参数分开可以,直接定义为类实例会提示参数“item”缺少值 


    [WebMethod]
    public string HelloWorld(string name,int age/*DataItem item*/)
    {
        //return item.name + "-" + item.age;
        return name+"--"+age;
    }

你的jquery 发送请求的代码设置了content-Type为application/json了吗


    $.ajax({
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({name:'showbo',age:10}),
        type:'POST',
        url:'/WebService.asmx/HelloWorld'
    });