web api Post 接收不到参数的问题

前端:

注意两个点:

1. contentType: "application/json" 请求的格式是Json

2. 要用JSON.stringify(customer)序列化对象成str传递

$(function () {
    var customer = {contact_name :"Scott",company_name:"HP"};
    $.ajax({
        type: "POST",
        data :JSON.stringify(customer),
        url: "api/Customer",
        contentType: "application/json"
    });
});

 

api:

注意frombody 的接收的参数只能是一个 ,不能定义多个 比如 String id ,string name

public class CustomersController : ApiController {

  public object Post([FromBody] Customer customer)
        {
            return Request.CreateResponse(HttpStatusCode.OK,
            new
            {
                customer = customer
            });
        }
    }
}

public class Customer
    {
        public string company_name { get; set; }
        public string contact_name { get; set; }
     }

 

posted @ 2016-11-14 15:32  晓明的哥哥  阅读(4687)  评论(0编辑  收藏  举报