POST请求
1)通过Request.Body原始流获取Body信息
控制器接收代码:
#region 根据body流读取body信息 [HttpPost] public async Task<IActionResult> GetRequestInfoByBody() { this.HttpContext.Request.EnableBuffering(); // 允许多次读取请求体 var stream = Request.Body; if (stream != null) { using (var reader = new StreamReader(stream, Encoding.UTF8)) { string body =await reader.ReadToEndAsync(); var bodyModel = Newtonsoft.Json.JsonConvert.DeserializeObject<OneBodyModel>(body); } } return Json(new { isSuccess=true }); } #endregion
客户端发送Ajax请求示例:
var xhr = new XMLHttpRequest(); xhr.open('post', '/Home/GetRequestInfoByBody'); xhr.responseType = 'json'; xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8'); xhr.send(JSON.stringify({ code: 1, msg: 'Test', data: [1, 2, 3] })); xhr.onloadend = function () { if (this.status == 200) { alert('ok'); } }
测试:
参考链接
https://www.cnblogs.com/kklldog/p/core-mvc-modelbind.html