ajax如何取得servlet中已经取得的数据库结果集(json格式)?

Flow_Shadow 2014-02-13 06:22:58
RT
  JSONArray json = JSONArray.fromObject(list); 
resp.setCharacterEncoding("gbk");
PrintWriter pw = resp.getWriter();

pw.print(json);


我已经做到这一步,访问能返回Json格式的数据,我现在想通过ajax取得这些数据,该怎么做,对ajax一点不熟悉啊,能来段代码吗?这几天吃不好睡不好的弄那个这个了
...全文
1444 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxbccsu 2014-02-28
  • 打赏
  • 举报
回复
引用 7 楼 lxbccsu 的回复:
其实JS是通过XMLHttpRequest对象来支持Ajax,而且所有现代浏览器均支持 XMLHttpRequest 对象(当然IE5和IE6使用的是ActiveXObject); 另外JSON就是一个字符串,当Servlet返回这个字符串后,剩下的就是前端Ajax怎么解析它了; 而XMLHttpRequest有个属性responseText,它就是用来 获得字符串形式的响应数据,所以剩下的就是: 读取 JSON 字符串 用 eval() 处理 JSON 字符串 根据你的代码示例,你返回的应该是一个数组,可以这样解析:

<script type="text/javascript">
function loadJSON(){
    var xmlhttp;
    if (window.XMLHttpRequest){
         xmlhttp = new XMLHttpRequest();
     }else{
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }

    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            var x;
            var strText = "";
            var jsonResult = eval("(" + xmlhttp.responseText + ")");
        	//var jsonResult = JSON.parse(xmlhttp.responseText);
        	for(x in jsonResult){
            	if(x == 0)
        			strText = jsonResult[x].firstName + ", " + jsonResult[x].lastName;
            	else
            		strText = strText + "; " + jsonResult[x].firstName + ", " + jsonResult[x].lastName;
            }
			
           alert(strText);
         }
   }
   
   xmlhttp.open("post","写上实际的URL", true);
   xmlhttp.send();
}

</script>
当然现代浏览器都提供了原生的 JSON 支持,你可以直接用JSON 解析器解析了,就是上面注释的代码;
注意: strText = jsonResult[x].firstName + ", " + jsonResult[x].lastName; 这里要写成你实际对象的字段名字;
lxbccsu 2014-02-28
  • 打赏
  • 举报
回复
其实JS是通过XMLHttpRequest对象来支持Ajax,而且所有现代浏览器均支持 XMLHttpRequest 对象(当然IE5和IE6使用的是ActiveXObject); 另外JSON就是一个字符串,当Servlet返回这个字符串后,剩下的就是前端Ajax怎么解析它了; 而XMLHttpRequest有个属性responseText,它就是用来 获得字符串形式的响应数据,所以剩下的就是: 读取 JSON 字符串 用 eval() 处理 JSON 字符串 根据你的代码示例,你返回的应该是一个数组,可以这样解析:

<script type="text/javascript">
function loadJSON(){
    var xmlhttp;
    if (window.XMLHttpRequest){
         xmlhttp = new XMLHttpRequest();
     }else{
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }

    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
            var x;
            var strText = "";
            var jsonResult = eval("(" + xmlhttp.responseText + ")");
        	//var jsonResult = JSON.parse(xmlhttp.responseText);
        	for(x in jsonResult){
            	if(x == 0)
        			strText = jsonResult[x].firstName + ", " + jsonResult[x].lastName;
            	else
            		strText = strText + "; " + jsonResult[x].firstName + ", " + jsonResult[x].lastName;
            }
			
           alert(strText);
         }
   }
   
   xmlhttp.open("post","写上实际的URL", true);
   xmlhttp.send();
}

</script>
当然现代浏览器都提供了原生的 JSON 支持,你可以直接用JSON 解析器解析了,就是上面注释的代码;
Mxreng 2014-02-16
  • 打赏
  • 举报
回复
//要请求的一级机构JSON获取页面
         $.getJSON("loadDistrictAndStreetServletAndTypes?method=getAllDistrict",function (data) {
         //对请求返回的JSON格式进行分解加载
         $(data).each(function () {
             $("#district").append($("<option/>").text(this.name).attr("value",this.id));
            });
         });
teemai 2014-02-14
  • 打赏
  • 举报
回复
前台解析json太爽了,直接obj.key的形式
tony4geek 2014-02-14
  • 打赏
  • 举报
回复
ghostkngiht 2014-02-14
  • 打赏
  • 举报
回复

 $.ajax({
    url: url地址,
    data: {
	参数1: 值1,
	参数2: 值2
    },
    success: function(data) {
	var array = eval('(' + data + ')');//处理后的json
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
	alert(textStatus);
	alert(errorThrown);
    }
});
简易人 2014-02-13
  • 打赏
  • 举报
回复
楼主,如果是ajax请求,返回用json格式返回就ok。ajax的回调函数都可以读取后台返回的数据
yktd26 2014-02-13
  • 打赏
  • 举报
回复
ajax获取只需要客户端更改比如用
jQuery.getJSON(url, function(data){console.log(data)});

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧