gpt4 book ai didi

java - AngularJS + Spring : 415 unsupported media type

转载 作者:行者123 更新时间:2023-11-30 04:05:12 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com