review0617-4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AJAX测试</title>
</head>
<body>
<input type="button" id="btnLoad" value="加载">
<div id="divContent"></div>
</body>
<script type="text/javascript">
document.getElementById('btnLoad').onclick = function(){
// 1、创建xmlhttprequest对象
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject('Microsoft.xmlHttp');
}
// 2、发送AJAX请求
xmlHttp.open('GET','http://128.0.0.1/content',true);
xmlHttp.send();
// 3、处理服务器响应
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var t = xmlHttp.responseText;
document.getElementById('divContent').innerHTML = t;
}
}
}
</script>
</html>
review0617-5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AJAX测试2</title>
</head>
<body>
<div id="container"></div>
</body>
<script type="text/javascript">
// 1、创建xmlhttprequest对象
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject('Microsoft.xmlHttp');
}
console.log('first');
// 2、发送AJAX请求
xmlHttp.open('GET','http://127.0.0.1:8080/news_list',true);
xmlHttp.send();
console.log('second');
// 3、处理服务器响应
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var t = xmlHttp.responseText;
// console.log(typeof t);
var json = JSON.parse(t);
// console.log(json);
var html = '';
for (var i=0;i<json.length;i++){
var news = json[i];
html = html + '<h1>' + news.title + '</h1>';
html = html + '<h2>' + news.date + ' ' + news.source + '</h2>';
html = html + '</hr>';
}
document.getElementById('container').innerHTML = html;
}
}
console.log('third');
</script>
</html>