Resource interpreted as Document but transferred with MIME type application/json

使用谷歌Chrome浏览器报错:Resource interpreted as Document but transferred with MIME type application/json: "http://localhost:8080/lzzcms/bak/trueAddContnet".   jquery.easyui.min.js:6844,截图如下:

Resource interpreted as Document but transferred with MIME type application/json

    先说下出现报这个错误的场景:前端框架是easyui,通过easyui的$('#toAdd_form').form()定义了ajax表单,然后使用easyui的$('#toAdd_form').submit()进行了图片上传操作,form的类型是enctype="multipart/form-data",我起初怀疑是easyui的原因,改用jquery-form.js来通过ajax的方式上传图片,依然是报Resource interpreted as Document but transferred with MIME type application/json这种错误。为什么呢?

        这是因为针对enctype="multipart/form-data"类型的form,我们知道如果不借助插件,仅仅想通过jquery来实现文件的ajax上传的话,是不行的,当enctype="multipart/form-data"类型的表单借助其他插件实现ajax上传的时候,enctype="multipart/form-data"类型的表单期待的返回值的MINE类型依然是text/html类型的,而不是ajax返回的application/json类型,如果你的程序中默认返回的是application/json类型,就会出现上边的警告,怎么解决呢?

比如有如下代码,我们知道返回值类型是application/json,就会报上边的错误:

@RequestMapping(value="/trueAddContnet") @ResponseBody
	public Map<String, Object> trueAddContnet(@RequestParam("toAdd_comm_thumbpic_file") MultipartFile file,
	HttpServletRequest request){
		Map<String, Object> returnMap=new HashMap<String, Object>();
		String ret=null;
		try {
			ret = contentInfoService.trueAddContnet(file,request);
			returnMap.put("status", "success");
			if (ret!=null) {//提交的是缩略图
				returnMap.put("info", ret);
			}else{
				returnMap.put("info", "添加文档成功");
			}
		} catch (Exception e) {
			logger.error("添加文档出错", e);
			returnMap.put("status", "error");
			returnMap.put("info", "添加文档或缩略图出错");
		}
		return returnMap;
	}

我们可以把返回的Map<String,Object>转为字符串,通过返回字符串的方式去掉上边的烦人警告,上边代码改写为:

Resource interpreted as Document but transferred with MIME type application/json

当然了,在前端如果想要获取json类型的数据,就要使用$.parseJSON(data);把data字符串转为json对象才可以了。