1、以Json格式传给后台的Controller
var data = { "UserId": Uid, "QID": Qid, "Age": age, "Sex": sex, }; var json = "[" + JSON.stringify(data) + "]"; $.ajax({ url: "/Home/User", type: 'post', data: { data: json }, cache: false, success: function (text) { Console.log(text); }, error: function (jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText); } });
后台Controller接收
public ActionResult UpdateQuestion(string data) { var question= = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject<user>(data); }
2、使用new FormData
2.1 HTML页面代码
<form action="/Home/User" id="SubmitForm" method="post" class="form-horizontal" enctype="multipart/form-data"> <div class="submit_bar"> <input type="button" class="btn" name="" value="提交"> </div> </form>
2.2 JS代码,当点击“提交”按钮时触发
var from = new FormData($("#SubmitForm")[0]); $.ajax({ type: "POST", url: "/Home/User", data: from, processData: false,//必须为 false contentType: false,//必须为 false success: function (data) { Console.log(data); }, Error: function (e) { console.log(e); } });
2.3 后台代码
[HttpPost] public async Task<IActionResult> User(UserDTO Dto) { }