//1.創建XMLHTTP對象
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//2.發送ajax請求
xmlhttp.open("GET","/ajax/Ajax",true);
xmlhttp.send();
//3.服務器處理響應
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readystate == 4 && xmlhttp.status == 200){
var text = xmlthhp.responseText;
console.log(text);
var json = JSON.parse(text);
var html = "";
document.getElementById("staff_list").onclick = function(){
for(var i=0; i
var staffs = json[i];
html = html+"
"+staffs.name+"
"}
document.getElementById("div").innerHTML = html;
}
}
}
-----------------------------------
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 創建員工對象存放在list集合內
List list = new ArrayList();
list.add(new Staff("小紅","經理","人事部"));
list.add(new Staff("小白","職員","技術部"));
list.add(new Staff("小藍","總裁","管理部"));
//以JOSN的格式把對象轉為JOSN格式字符串
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(jsonString);
}
在这里输入代码,可通过选择【代码语言】突出显示