对于简单类型,在服务器端:
public void Post([FromBody]string name){}在客户端,您只需定义是否要以json格式发送:
var dataJSON = "test"; $('#testPostMethod').bind("click", GeneralPost); function GeneralPost() { $.ajax({ type: 'POST', url: '/api/NewRecipe', data: JSON.stringify(dataJSON), contentType: 'application/json; charset=utf-8', dataType: 'json' }); }如果要使其以复杂类型工作,则应从服务器端定义:
public class RecipeInformation{ public string name { get; set; }}public class ValuesController : ApiController{ public void Post(RecipeInformation information) { }}从客户端:
var dataJSON = { name: "test" }; $('#testPostMethod').bind("click", GeneralPost); function GeneralPost() { $.ajax({ type: 'POST', url: '/api/NewRecipe', data: JSON.stringify(dataJSON), contentType: 'application/json; charset=utf-8', dataType: 'json' }); }


