我正在尝试通过Ajax下载CSV文件。
restController代码
@RequestMapping(value = "audit/search/csv", method = RequestMethod.GET)
public ModelAndView auditSearchCsv(/*@RequestBody JSONObject jsonParam,*/ HttpServletRequest request) throws ParseException {
final Enumeration<?> enumber = request.getParameterNames();
final HashMap<String, Object> param = new HashMap<String, Object>();
while (enumber.hasMoreElements()) {
final String key = enumber.nextElement().toString();
final String value = request.getParameter(key);
param.put(key, value);
}
final JSONObject jsonParam = (JSONObject) new JSONParser().parse((String) param.remove(JSONPAdvice.KEY_JSON_PARAM));
String index = (String)jsonParam.get("index");
APIResponseVO vo = requestSearch(jsonParam);
ApiCsvView view = new ApiCsvView();
view.setFileName(index+"_log.csv");
ModelAndView model = new ModelAndView();
model.setView(view);
model.addObject("data", vo.getRows());
model.addObject("index", index);
model.addObject("result", "OK");
return model;
}
jsp代码
$。ajax({
url: 'http://localhost:8080/search/jsonp?jsonParam=' + encodeURI(jsonParam),
dataType: 'jsonp',
success: function(data) {
console.log(JSON.stringify(data));
},
error: function(xhr) {
console.log('fail : ', xhr);
}
});
在jsp中,我将dataType设置为“ jsonp”,并对所有参数进行了编码并发出了请求。
在内部Controller方法中,modelAndView对象设置了ApiCsvView,它将继承AbstractView类作为视图,并且该方法返回了它。
但是,它从未下载过csv文件,但我在浏览器控制台上看到一条错误消息,内容为“失败:对象{readyState:4,responseText:等等等等,状态:200,statusText:“ parsererror”} 我该如何解决这个问题?