jQuery Ajax传递数组到asp.net web api参数为空
前端:
var files = []; files.push({ FileName: "1.jgp", Extension: ".jgp", FileType: 2 }); files.push({ FileName: "2.png", Extension: ".png", FileType: 2 }); files.push({ FileName: "3.bpm", Extension: ".bpm", FileType: 2 }); $.ajax({ url: "/api/file/uploadservice", type: "post", dataType: "json", data: { "": files }, success: function (res) { alert(res); }, error: function (xhr ,err) { alert(err); } });
后台:
/// <summary> /// 批量上传文件 /// </summary> /// <param name="files"></param> /// <returns></returns> [HttpPost] [Route("uploadservice")] public HttpResponseMessage AppendFiles(List<FileInfo> files) { //上传文件处理 }
结果,后台中接收到的files为空
原因:jQuery的怪癖
解决:
.ajax({ url: "/api/file/uploadservice", type: "post", dataType: "json", data: { "": files }, success: function (res) { alert(res); }, error: function (xhr ,err) { alert(err); } });
参考文献: