服务器代码
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/first', (req, res) => {
res.send('hello ajax')
});
app.get('/responseData', (req, res) => {
res.send({ "name": "张三" });
});
app.get('/get', (req, res) => {
res.send(req.query);
});
app.post('/post', (req, res) => {
res.send(req.body)
});
app.post('/json', (req, res) => {
res.send(req.body)
});
app.get('/readystate', (req, res) => {
res.send('hello')
});
app.get('/error', (req, res) => {
res.status(400).send('not ok')
});
app.get('/cache', (req, res) => {
fs.readFile('./test.txt', (err, result) => {
res.send(result);
})
});
app.listen(3000);
console.log('服务器启动成功');
1.第一次使用ajax
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://localhost:3000/first');
xhr.send();
xhr.onload = function() {
console.log(xhr.responseText);
}
</script>
</body>
</html>
2.处理服务器返回的JSON数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://localhost:3000/responseData');
xhr.send();
xhr.onload = function() {
console.log(xhr.responseText);
console.log(typeof xhr.responseText);
var responseText = JSON.parse(xhr.responseText);
console.log(responseText);
var str = "<h2>" + responseText.name + "</h2>"
document.body.innerHTML = str;
}
</script>
</body>
</html>
3.传递get请求参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>
<input type="text" name="" id="username">
</p>
<p>
<input type="text" name="" id="age">
</p>
<p>
<input type="button" value="提交" id="btn">
</p>
<script>
var btn = document.querySelector('#btn');
var username = document.querySelector("#username");
var age = document.querySelector("#age");
btn.onclick = function() {
var xhr = new XMLHttpRequest();
var nameValue = username.value;
var ageValue = age.value;
var params = 'username=' + nameValue + '&age=' + ageValue;
xhr.open("get", "http://localhost:3000/get?" + params);
xhr.send();
xhr.onload = function() {
console.log(xhr.responseText);
}
}
</script>
</body>
</html>
4.传递post请求参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>
<input type="text" name="" id="username">
</p>
<p>
<input type="text" name="" id="age">
</p>
<p>
<input type="button" value="提交" id="btn">
</p>
<script>
var btn = document.querySelector('#btn');
var username = document.querySelector("#username");
var age = document.querySelector("#age");
btn.onclick = function() {
var xhr = new XMLHttpRequest();
var nameValue = username.value;
var ageValue = age.value;
console.log(nameValue);
console.log(ageValue);
var params = 'username=' + nameValue + '&age=' + ageValue;
xhr.open('post', 'http://localhost:3000/post');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(params);
xhr.onload = function() {
console.log(xhr.responseText);
}
}
</script>
</body>
</html>
5.向服务器传递JSON类型的参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('post', 'http://localhost:3000/json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
name: 'list',
age: 50
}));
xhr.onload = function() {
console.log(xhr.responseText);
}
</script>
</body>
</html>
6.获取服务器端响应数据的另一种方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var xhr = new XMLHttpRequest();
console.log(xhr.readyState);
xhr.open('get', 'http://localhost:3000/readystate');
console.log(xhr.readyState);
xhr.onreadystatechange = function() {
console.log(xhr.readyState);
if (xhr.readyState == 4) {
console.log(xhr.responseText);
}
}
xhr.send();
</script>
</body>
</html>
7.Ajax错误处理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btn">发送ajax请求</button>
<script>
var btn = document.querySelector('#btn')
btn.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://localhost:3000/error');
xhr.send();
xhr.onload = function() {
console.log(xhr.responseText);
if (xhr.status == 400) {
console.log('请求出错');
};
}
xhr.onerror = function() {
alert('网络中断无法发送ajax请求')
}
};
</script>
</body>
</html>
8.ajax缓存
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btn">发送ajax请求</button>
<script>
var btn = document.querySelector('#btn')
btn.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.open('get', 'http://localhost:3000/cache?t' + Math.random());
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText)
}
}
};
</script>
</body>
</html>