本人在前端接收到的数据为:

且在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;
}
}
在前端项目中,遇到后端返回的数据显示为[object,Object],导致错误。问题源于数据格式不正确。尝试将ajax dataType设为"text",但只会显示文字信息。另一种解决方案是在后端将数据转换为期望的类型,例如通过renderString(response, flag)函数,根据flag值来确定转换的目标数据类型。"
102853151,8497928,JMeter错误排查与解决,"['性能测试', 'JMeter插件', 'HTTP协议', '错误调试', '分布式']

1826

被折叠的 条评论
为什么被折叠?



