• 全部
  • 问答

求教jquery读取json数组undefined的问题

陈小潮 2015-12-25 08:57:39
各位,小弟请教个问题

我在前台通过jquery ajax访问后台,读取数据库,返回查询结果列表,前台在获取这个json数组的时候,取到的值都是undefined的,请问是怎么回事?应该怎么写?代码如下


js
$(document).ready(function() {
$("#btn").click(function(){
$.ajax( {
url : "test.action", // 数据发送方式
type : "post",
dataType : "json",// 接收数据格式
timeout : 20000,// 设置请求超时时间(毫秒)。
success : function(data) { // 请求成功后回调函数。
//读取权限等信息再跳转页面
var d = eval("("+data+")");
$.each(result, function (i, n) {
alert(n.user_no);
});
// $.each(d, function(i, item) {
// $("#info > ul").append("<li>" + item.user_no +"---------"+item.last_login_time+"</li>");
// })

},
error : function(e) {
alert(e);
}
})
})
})



后台java代码


package com.sunyard.cop.ccs.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.sunyard.cop.ccs.common.SQLExecutor;
import com.sunyard.encrypt.impi.EncryptImp;

public class TestAction implements Action {


Map<String,String> map = new HashMap<String,String>();

private JSONArray list;//我要返回给页面的List
private String result;

public String execute() throws Exception {
// TODO Auto-generated method stub
SQLExecutor dbHelper = new SQLExecutor();
dbHelper.open();
String querySql = "select * from sm_users_tb";
ArrayList array = dbHelper.queryForList(querySql, null);
list = new JSONArray();
list=JSONArray.fromObject(array);
System.out.println("list:"+list);
return SUCCESS;
}


public JSONArray getList() {
return list;
}


public void setList(JSONArray list) {
this.list = list;
}


public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}


}


其中后台的返回 list 控制台输出的内容为

[
{"TELLERLVL":1,"LOGIN_STATE":5,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"admin","USER_NAME":"系统超级管理员","ORGAN_NO":"000000","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20121220164126","PASSWORD":"66UbaZVbHvA=","USER_STATUS":0},
{"TELLERLVL":0,"LOGIN_STATE":99,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000001","USER_NAME":"系统参数管理员","ORGAN_NO":"000001","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"WWzeO3tPUHo=","USER_STATUS":0},
{"TELLERLVL":2,"LOGIN_STATE":99,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"sunds","USER_NAME":"业务参数管理员","ORGAN_NO":"000001","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"aDN18k+Z9xU=","USER_STATUS":1},
{"TELLERLVL":1,"LOGIN_STATE":98,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000000","USER_NAME":"综合管理员","ORGAN_NO":"000001","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20121220164126","PASSWORD":"4K48Oeeq1B8=","USER_STATUS":0},
{"TELLERLVL":1,"LOGIN_STATE":98,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000002","USER_NAME":"柜员2","ORGAN_NO":"000001","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"MZHMgKIx/wE=","USER_STATUS":0},
{"TELLERLVL":6,"LOGIN_STATE":99,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000003","USER_NAME":"柜员3","ORGAN_NO":"000001","USER_ENABLE":"0","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"Yl0u/AYbePY=","USER_STATUS":0},
{"TELLERLVL":3,"LOGIN_STATE":98,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000004","USER_NAME":"柜员4","ORGAN_NO":"20001","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"3F2zGT4dOKg=","USER_STATUS":0},
{"TELLERLVL":7,"LOGIN_STATE":99,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000005","USER_NAME":"柜员5","ORGAN_NO":"20001","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"t6ij5i/b30E=","USER_STATUS":0},
{"TELLERLVL":5,"LOGIN_STATE":99,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000006","USER_NAME":"柜员6","ORGAN_NO":"20002","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"a4B186C2ieo=","USER_STATUS":0},
{"TELLERLVL":6,"LOGIN_STATE":99,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000007","USER_NAME":"柜员7","ORGAN_NO":"20002","USER_ENABLE":"2","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"NmxKYXsLhfE=","USER_STATUS":0},
{"TELLERLVL":7,"LOGIN_STATE":98,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000008","USER_NAME":"柜员8","ORGAN_NO":"20003","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"1uhqZ8NEqNI=","USER_STATUS":0},
{"TELLERLVL":8,"LOGIN_STATE":5,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000009","USER_NAME":"柜员9","ORGAN_NO":"20003","USER_ENABLE":"0","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"P0pyNC7MGuE=","USER_STATUS":0},
{"TELLERLVL":9,"LOGIN_STATE":98,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000010","USER_NAME":"柜员A","ORGAN_NO":"20004","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"otNA3VDC6CY=","USER_STATUS":0},
{"TELLERLVL":4,"LOGIN_STATE":5,"LOGIN_MODE":"1","LAST_LOGIN_TIME":"20121220101010","USER_NO":"000011","USER_NAME":"柜员B","ORGAN_NO":"20004","USER_ENABLE":"1","LAST_LOGOUT_TIME":"20121220111111","LAST_MODI_DATE":"20120514155225","PASSWORD":"E1Xmv+0Hq+A=","USER_STATUS":0}
]



请高人指点,小弟不胜感激
...全文
384 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
服务器端序列化为json格式字符串就行,多少个list都可以。你建立一个类,类里面多申明几个list对象,然后序列化这个类
引用 4 楼 chch010520 的回复:
[quote=引用 3 楼 showbo 的回复:] 注意区分大小写。。
n.user_no
==>
n.USER_NO
而且你都制定dataType为json了,data就是json对象,你的eval那句应该报错才对。,不需要eval了 dataType : "json",// 接收数据格式 success : function(data) { var d = eval("("+data+")");
谢谢,完美解决了。 另外想再请教,如果jquery调用后台查询,会查询两张表,返回两个list ,请问在jquery中怎么处理这两个list呢?[/quote]
回复
陈小潮 2015-12-27
引用 3 楼 showbo 的回复:
注意区分大小写。。
n.user_no
==>
n.USER_NO
而且你都制定dataType为json了,data就是json对象,你的eval那句应该报错才对。,不需要eval了 dataType : "json",// 接收数据格式 success : function(data) { var d = eval("("+data+")");
谢谢,完美解决了。 另外想再请教,如果jquery调用后台查询,会查询两张表,返回两个list ,请问在jquery中怎么处理这两个list呢?
回复
注意区分大小写。。
n.user_no
==>
n.USER_NO
而且你都制定dataType为json了,data就是json对象,你的eval那句应该报错才对。,不需要eval了 dataType : "json",// 接收数据格式 success : function(data) { var d = eval("("+data+")");
回复
陈小潮 2015-12-25
引用 1 楼 zzgzzg00 的回复:
result变量哪来的阿?
错了 应该的d
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	
$(document).ready(function() {
 $("#btn").click(function(){   
        $.ajax( {
            url : "test.action",  // 数据发送方式
            type : "post",
            dataType : "json",// 接收数据格式
            timeout : 20000,// 设置请求超时时间(毫秒)。
            success : function(data) { // 请求成功后回调函数。
                  //读取权限等信息再跳转页面
                var d = eval("("+data+")");
                 $.each(d, function (i, n) {
                 alert(n.user_no);
                    });
//                   $.each(d, function(i, item) {
//                 $("#info > ul").append("<li>" + item.user_no +"---------"+item.last_login_time+"</li>");
//                   })
         
             },
             error : function(e) {
                  alert(e);
             }
        })
    })
})
这样才对,但是还是undefined
回复
似梦飞花 2015-12-25
result变量哪来的阿?
回复
相关推荐
发帖
JavaScript
创建于2007-09-28

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2015-12-25 08:57
社区公告
暂无公告