使用express 建立一个JavaScript后端的程序:
const express = require('express');
//2 创建应用对象
const app = express();
//3创建路由规则
// request对请求报文的封装 response 对响应报文的封装
app.get('/server', (request, response)=>{
//设置响应头 设置允许跨域
response.setHeader('Access-Control-Allow-Origin', '*');
//设置响应体
response.send('Hello Ajax');
});
app.post('/server', (request, response)=>{
//设置响应头 设置允许跨域
response.setHeader('Access-Control-Allow-Origin', '*');
//设置响应体
response.send('Hello Ajax POST');
});
//4监听端口服务
app.listen(8000,()=>{
console.log("服务器已经启动,正在监听8000----......");
});
使用html DOM实现前端接收
<!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>ajax GET请求</title>
<style>
#result {
width: 200px;
height: 100px;
border: solid 1px #90b;
}
</style>
</head>
<body>
<button type="button">点击我发送请求</button>
<div id="result"></div>
<script>
//获取button元素
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById("result");
//绑定事件
btn.onclick = function(){
//console.log('test');
//1,创建对象
const xhr = new XMLHttpRequest();
//2,初始化,设置请求方法和URL
xhr.open('GET','http://127.0.0.1:8000/server');
//3,发送
xhr.send();
//4,事件绑定 处理服务端返回的结果
//on when 当...的时候
//readystate 是xhr对象的属性,表示状态0 1 2 3 4
//初始化值为0 open为1 send为2 返回部分结果为3 返回完毕为4
//change 改变次数
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
//判断响应码状态200 404 403 401 500
//2xx成功
if (xhr.status >= 200 && xhr.status<300) {
//处理服务端返回结果 行 头 空行 体
//响应行
// console.log(xhr.status);//状 态码
// console.log(xhr.Text);//状态字符串
// console.log(xhr.getAllResponseHeaders()); //返回所有响应头
// console.log(xhr.response); //响应体
//设置result的文本
result.innerHTML = xhr.response;
}
else{
}
}
}
}
</script>
</body>
</html>
使用jquery 读取:
<!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>ajax GET请求(查询,获取数据)</title>
<style>
#result {
width: 200px;
height: 100px;
border: solid 1px #90b;
}
</style>
<script src="../jquery/jquery-3.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<button id="btn1">点击我发送请求</button>
<div id="result"></div>
<script>
$("#btn1").click(function(){
$.ajax({
type:"get",
url:"http://127.0.0.1:8000/server",
data:{},
//dataType:"json",//预期服务器返回的数据类型, 不用本地转换了
success:function(data){
$("#result").append(data);
console.log(data);
}
})
})
</script>
</body>
</html>
使用jQuery Post方法:
<!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>Ajax Post请求(修改)</title>
<style>
#result {
width: 200px;
height: 100px;
border: solid 1px #903;}
</style>
<script src="../jquery/jquery-3.4.1.min.js"></script>
</head>
<body>
<div id = "result"></div>
<script>
//const result =document.getElementById("result");
//绑定事件
$(function(){
$("#result").mouseover(function(){
$.ajax({
type:"post",
url:"http://127.0.0.1:8000/server",
data:{},
//dataType:"json",//预期服务器返回的数据类型, 不用本地转换了
success:function(data){
$("#result").append(data);
console.log(data);
}
})
})
})
</script>
</body>
</html>