发布网友 发布时间:2022-04-30 07:40
共3个回答
懂视网 时间:2022-04-30 12:01
("departmentID"); DataBaseHandle dataBaseHandle=new DataBaseHandle("commonUser", "common", "jdbc:mysql://127.0.0.1:3306/firstwebsite"); String SQL="select * from speciality where departmentID=‘"+departmentID+"‘"; String json="{‘specialities‘:["; response.setContentType("text/html; charset=utf-8"); response.setCharacterEncoding("UTF-8"); try { ResultSet rs=dataBaseHandle.exeuteSQL(SQL); while(rs.next()) { json=json+"{‘"+"specialityID‘:‘"+rs.getString(1)+"‘,"+"‘SPname‘:‘"+rs.getString(2)+"‘},"; System.out.println(rs.getString(1)+rs.getString(2)); } json=json.substring(0,json.length()-1); json=json+"]}"; PrintWriter pwriter=response.getWriter(); pwriter.print(json); rs.close(); dataBaseHandle.closeResourse(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }js解析json,获得返回的内容
function updateSpeciality(str)
{
var xmlhttp;
if (str=="")
{
alert("请选择非空的值!");
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var dataObj=eval("("+xmlhttp.responseText+")");//转换为json对象
for(var o in dataObj.specialities)
{
alert(dataObj.specialities[o].specialityID);
alert(dataObj.specialities[o].SPname);
}
}
}
xmlhttp.open("GET","/First/servlet/GetMessage?departmentID="+str,true);
xmlhttp.send();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
ajax查询数据库,服务器传回json字符串,js解析json
标签:ajax 服务器 json js
热心网友 时间:2022-04-30 09:09
(1)如果是jquery封装的ajax方法,可以配置好参数,设置返回数据的类型为json,这样回调函数里面接到的就是一个json对象,可以直接用data.xxx取出里面的属性.热心网友 时间:2022-04-30 10:27
$.getJSON("../方法", function (data) {