前端:
注意兩個點:
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; } }
