http post 在上传多部分数据时出错
var formData = new FormData();
formData.append("startDate",$("#startDate").val());
formData.append("File1",$("input[name='file']")[0].files[0]);
formData.append("File2",$("input[name='file2']")[0].files[0]);
$http.post("sampleurl",formData,
{ headers : 'Content-Type' : undefined},
transformRequest : angular.identity
}).then(function(data){
alert(data);
});
}
我的服务器端代码是
@RequestMapping(value = "sampleurl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
public @ResponseBody
Response createSomething(
@RequestBody Request request,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
// code here
}
这里出了什么问题,我一直在寻找解决方案,请帮我找到解决方案
最佳答案
http 错误 415 表示请求内容的格式不正确。
Spring MVC“@RequestBody”需要一个 json 正文(Content-Type 等于“application/json”),并且您明确将 Content-Type 设置为未定义。
解决方案是在发布请求中将内容类型设置为“application/json”或删除 @RequestBody 注释。
看来你尝试上传文件,删除@RequestBody注释会更容易。
关于java - AngularJS + Spring : 415 unsupported media type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20950346/