ajax如何取得servlet中已经取得的数据库结果集(json格式)? ajax获取只需要客户端更改比如用jQuery.getJSON(url, function(data){console.log(data)}); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主,如果是ajax请求,返回用json格式返回就ok。ajax的回调函数都可以读取后台返回的数据 $.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); }}); json 前台解析json太爽了,直接obj.key的形式 //要请求的一级机构JSON获取页面 $.getJSON("loadDistrictAndStreetServletAndTypes?method=getAllDistrict",function (data) { //对请求返回的JSON格式进行分解加载 $(data).each(function () { $("#district").append($("<option/>").text(this.name).attr("value",this.id)); }); }); 其实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;这里要写成你实际对象的字段名字; 如何知道我当前的jsp的版本 acsii码转换 高分求代码解答! 怎样在JTextArea中使行距和字符的左右间距都为零? 请问如何利用java生成这样的时间: Tue, 30 Dec 2003 03:08:35 GMT JCreator2.5 中对于中文显示的BUG怎么解决? 找到心爱的女孩了~开心~散分~~~~~ applet中的swing可不可以用Look and Feel 急~~~~ 哪位知道那里有JAVA类库完全手册下载。 算法面试题 java面试题(都进来尝试下)
$.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);
}
});
$.getJSON("loadDistrictAndStreetServletAndTypes?method=getAllDistrict",function (data) {
//对请求返回的JSON格式进行分解加载
$(data).each(function () {
$("#district").append($("<option/>").text(this.name).attr("value",this.id));
});
});
读取 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;
这里要写成你实际对象的字段名字;