AngularJS +弹簧:415不支持的媒体类型 [英] AngularJS + Spring: 415 unsupported media type

查看:262
本文介绍了AngularJS +弹簧:415不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP POST越来越错误上载多部分数据

http post is getting error while uploading multipart data

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);
    });
}

我的服务器端code是

my server side code is

@RequestMapping(value = "sampleurl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON)
    public @ResponseBody
    Response createSomething(
            @RequestBody Request request,
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) {
            // code here
    }

出了什么问题在这里,我stucked找到解决办法,请大家帮我想办法了。

what went wrong here, i am stucked to find the solution, please help me to find the solution

推荐答案

HTTP错误415意味着,请求内容是不是在适当的格式。

An http error 415 means that content of request is not in the appropriate format.

Spring MVC的'@RequestBody期待一个JSON体(有内容类型等于'应用程序/ JSON')和你的内容类型明确设置为undefined。

Spring MVC '@RequestBody' expect a json body (with a Content-Type equal to 'application/json') and you explicitly set your Content-Type to undefined.

解决的办法是设置内容类型为应用程序/ JSON在您的文章的请求或删除@RequestBody注释。

The solution is to set your content-type to 'application/json' in your post request or to remove @RequestBody annotation.

看来你尝试上传文件时,更容易将删除@RequestBody注释。

It seems that you try to upload files, the easier would be to remove @RequestBody annotation.

这篇关于AngularJS +弹簧:415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆