本人在前端接收到的数据为:
且在ajax的error方法中返回的,经过查看发现是返回的数据格式不正确导致的原因,在网上搜索到的方法有
① 将ajax的dataType的类型改为"text"类型的,但是改为text类型出现的就是上图所示的文字信息,否则页面显示[object,Object]
② 将数据在前端返回的时候进行直接的数据类型的转变,将本要返回的数据类型变为想要接收到的数据类型,方法为:
renderString(response, flag);
其中flag为要传递的值的类型,后台接受到的数据为flag
/**
* 客户端返回JSON字符串
* @param response
* @param object
* @return
*/
protected String renderString(HttpServletResponse response, Object object) {
return renderString(response, JsonMapper.toJsonString(object), "application/json");//将返回的数据设置为json格式
}
/**
* 客户端返回字符串
* @param response
* @param string
* @return
*/
protected String renderString(HttpServletResponse response, String string, String type) {
try {
response.reset();
response.setContentType(type);
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
return null;
} catch (IOException e) {
return null;
}
}