$.ajax({
url: '/Controller/Action',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ name: '張三', age: 25 }),
success: function (response) {
console.log(response);
}
});
在 ASP.NET MVC 中,可建立一個模型類別來對應 JSON 資料,讓模型繫結器自動將 JSON 轉換為物件。
public class UserModel {
public string Name { get; set; }
public int Age { get; set; }
}
[HttpPost]
public ActionResult Action(UserModel model) {
// 直接使用 model.Name 與 model.Age
return Json(new { success = true });
}
contentType: 'application/json',並使用 JSON.stringify 將物件轉為 JSON 字串。[FromBody] 屬性。

