AngularJS + Spring: 415 unsupported media type

血红的双手。 提交于 2019-12-11 13:51:24

问题


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

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
    }

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


回答1:


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

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

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

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




回答2:


You should be sending multipart/form-data and not undefined value in your Content-Type header (Accept header sent from client should be application/json). Also make sure that the method on the server side consumes this particular media type.



来源:https://stackoverflow.com/questions/20950346/angularjs-spring-415-unsupported-media-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!