ajax请求后台,后台有数据,前台得不到数据(json)
来源:互联网 发布:哈尔滨学院教务网络 编辑:程序博客网 时间:2024/06/05 20:36
问题一:后台能得到数据,前台却进入error(),取不到json数据,也没有错误(js报错)。
后台封装错误,注意json的格式。(就是因为后台json数据封装格式有问题,所以在前台一直拿不到数据)。
$.ajax({
type:"post",
url:"xxxxx.do",
dataType:"json",
success:function(data) {
alert(data);
},
error:function(e) {
}
});
$.post(
"url",{
data : data,
data1 : data1
},function(result) {
alert(result);
},"json"
);
问题二:在不用fastJson或者其他封装工具时,对要封装的对象的最小单位进行toString方法的重写,
boolean和数值类型不用加双引号。
/**重写entity中的toString方法**/
@Override
public String toString() {
// TODO Auto-generated method stub
return "{\"fileName\": \"" + this.fileName + "\",\"fileParentUrl\": \"" + this.fileParentUrl
+ "\",\"fileUrl\": \"" + this.fileUrl + "\",\"isDirectory\": " + this.isDirectory + ",\"size\": "
+ this.size + "}";
}
/**
* servlet
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<UserInfo> voList = new ArrayList<UserInfo>();
voList = dao.getUserInfos();
String json = "{\"data\":"+voList+"}";//封装json
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/json");
PrintWriter out = resp.getWriter();
out.print(json);
out.flush();
out.close();
System.out.println(json);
后台封装错误,注意json的格式。(就是因为后台json数据封装格式有问题,所以在前台一直拿不到数据)。
$.ajax({
type:"post",
url:"xxxxx.do",
dataType:"json",
success:function(data) {
alert(data);
},
error:function(e) {
}
});
$.post(
"url",{
data : data,
data1 : data1
},function(result) {
alert(result);
},"json"
);
问题二:在不用fastJson或者其他封装工具时,对要封装的对象的最小单位进行toString方法的重写,
boolean和数值类型不用加双引号。
/**重写entity中的toString方法**/
@Override
public String toString() {
// TODO Auto-generated method stub
return "{\"fileName\": \"" + this.fileName + "\",\"fileParentUrl\": \"" + this.fileParentUrl
+ "\",\"fileUrl\": \"" + this.fileUrl + "\",\"isDirectory\": " + this.isDirectory + ",\"size\": "
+ this.size + "}";
}
/**
* servlet
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<UserInfo> voList = new ArrayList<UserInfo>();
voList = dao.getUserInfos();
String json = "{\"data\":"+voList+"}";//封装json
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/json");
PrintWriter out = resp.getWriter();
out.print(json);
out.flush();
out.close();
System.out.println(json);
}
问题三:json封装
阅读全文
0 0
- ajax请求后台,后台有数据,前台得不到数据(json)
- 前台jquery+ajax向后台请求数据,后台返回json,前台显示
- ajax请求数据,后台提供数据返回json, 前台js拼html,回填
- Ajax请求后台数据
- ajax 请求后台数据
- ajax 前台后台传输数据
- ajax json 前台传数据到后台,后台接收并返回json 数据
- Ajax +SpringMVC 请求后台数据
- jstree 请求后台数据前台处理
- json 后台传数据到前台
- java 后台返回json数据给前台
- java 后台返回json数据给前台
- 前台后台的的json数据交互
- 将后台数据传到前台JSON应用
- json+easyUI后台传数据到前台
- 前台拼接json数据java后台接收
- 关于后台返回json数据给前台
- 后台数据map转json传到前台
- 【自用】Javanote170802(抽象类接口内部类小结)
- Volley网络请求框架
- PHP 语言结构与函数区别
- windows网络编程之select模型(改进)
- java实现判断集群中的主机是否在线
- ajax请求后台,后台有数据,前台得不到数据(json)
- datagrid combobox的显示值的问题
- 工具快捷键
- python之socket编程举例
- 解决log4cxx输出中文路径乱码问题
- [ JS 进阶 ] Repaint 、Reflow 的基本认识和优化
- 10版outlook主账户删不掉
- 【数据处理·Python】时间数据处理之Arrow库(转)
- Android学习之EventBus基础篇