使用序列化对象将ajax请求转换为json字符串,并在控制器中使用类对象接收json数据。
例如:
//在前端进行ajax请求时,将数据转换为json字符串 var dataObject = { name: "John", age: 30 }; $.ajax({ url: "Controller/Action", type: "POST", data: JSON.stringify(dataObject), dataType: "json", contentType: "application/json", success: function (result) { console.log(result); }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status); } });
//在控制器中使用类对象接收json数据 public class DataModel { public string Name { get; set; } public int Age { get; set; } }
public ActionResult Action(DataModel model) { //使用接收到的数据进行逻辑处理 return Json(result, JsonRequestBehavior.AllowGet); }