JS —— Http unsupported media type 415 错误

后端使用Spring 框架,接口为:

@ApiOperation(value = "添加表单元素型",httpMethod = "POST",notes = "添加表单元素",
    produces="application/json",tags = {"lims-service-tms"})
@ApiImplicitParam(paramType = "body",name = "field",value = "元素",dataType = "FieldVO",
    required = true)
@ResponseBody
@RequestMapping(value="/addBean",method=RequestMethod.POST)
public View addBean(@RequestBody(required=true) FieldVO field,HttpServletRequest req,
    HttpServletResponse resp,Model model){...}
注:@ApiOperation 等为swagger ui 工具定义的注解,其中有项produces 属性,不知这会不会影响ajax请求。


前端出错的 JS 代码:

$.ajax({
	url:url,type:type,data:JSON.stringify(field),dataType:"json",/*contentType:'application/json',*/
	xhrFields:{withCredentials:true},
	success:function(data){
		if(data.status){
			lims.notification('Y','',data.mes);
		}else{
			lims.notification('N','',data.mes);
		}
	},
	error:function(){
	        lims.notification('N','',"服务器离线,请刷新页面");
		}
});

解决:

去掉上面js 中contentType:"application/json"注释,即添加contentType属性。但奇怪的是,另一个系统请求相同的该接口时,可以不同带该参数,所以这应该不是根本原因,不过最好都带上吧!



你可能感兴趣的:(JS,错误整理,综合)