使用fetch函数发送请求
fetch() 全局对象 可以直接去调用,返回的结果是一个promise对象
参考:https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button>AJAX请求</button>
<script>
const btn = document.querySelector('button');
btn.onclick = function() {
fetch('http://127.0.0.1:8000/fetch-server?vip=10', {
// 请求方法
method: 'POST',
// 请求头
headers: {
name: 'yaya'
},
// 请求体
body: 'username=admin&password=admin'
}).then(response => {
// return response.text();
return response.json();
}).then(response => {
console.log(response);
})
}
</script>
</body>
</html>