Ajax 傳送formData 返回500
阿新 • • 發佈:2018-12-22
JS
function fileSubmit() { var formData = new FormData(); formData.append("file",$("#FileUpload")[0].files[0]); formData.append("str","hello"); $.ajax({ url: "jsonTest", data: formData, type: "Post", cache: false,//上傳檔案無需快取 processData: false,//用於對data引數進行序列化處理 這裡必須false contentType: false, //必須 success: function (data) { console.log(data) console.log("success") } }) }
Java後臺:
@RequestMapping("jsonTest") @ResponseBody public Map<String,String> jsonTest(HttpServletRequest request){ String str = request.getParameter("str"); System.out.println(str); Map<String,String> map=new HashMap<String, String>(); map.put("username","小文"); map.put("number","001"); return map; }
開發工具:IDEA
現象:
一直報500,後臺打斷點,但是就是跳轉不過去
解決辦法:
SpringMVC上傳檔案 需要用到兩個包
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
在Maven中新增後,開啟Project Structure