项目的springboot从1.5.10升级到1.5.16,发现老代码里ajaxSubmit的请求返回都为error了.
后台接口收到请求并正常处理和返回,没有任何异常。但前端接受到的返回体是个空对象。
前端请求代码:
<form id="uploadForm" action="/image/upload" method="post" enctype="multipart/form-data">
<input type="file" id="imageFile" name="imageFile" accept=".jpg,.jpeg,.png,.gif">
</form>
$("#uploadForm").ajaxSubmit({
success: function (data) {
alert("上传照片成功!");
},
error: function (data) {
alert("上传失败");
}
后端接收代码:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void upload(@RequestParam(value = "imageFile", required = false) MultipartFile imageFile,HttpServletResponse response){
imageService.saveUploadImage(imageFile);
PrintWriter out = null;
try {
response.setContentType("text/html; charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("X-Frame-Options", "SAMEORIGIN");
out = response.getWriter();
out.write("success");
out.flush();
} catch (Exception ex) {
throw new RuntimeException("write return string has error", ex);
} finally {
if (out != null)
out.close();
}
}
js返回内容(data):
接口返回头:
后将ajaxSubmit重写为ajax请求解决。
测试发现从1.5.11开始都会出现此问题。
猜测应该是与springboot安全相关的默认配置有关,因ajaxSubmit的实现原理是创建iframe来发送请求和接受返回
预计相关包:
org.springframework.boot:spring-boot-starter-web org.springframework.boot:spring-boot-starter-security