一、入门
1.1 AJAX
AJAX:Asynchronous JavaScript And XML,直译就是异步JS和XML
通过AJAX可以在浏览器中向服务器发送异步请求,最大的优势:无刷新获取数据。AJAX不是新的编程语言,而是一种将现有的标准组合在一起使用的新方式。|
1.2 XML和JSON
-
XML是可扩展标记语言
-
XML被设计用来传输和储存数据
-
与HTML类似,只不过HTML都是预定义标签,如p,h1,a,div等,而XML中没有预定义标签,都是自定义标签,如以下数据:name=“张三”; age=18; gender=“男”; 用XML表示为:
<student> <name>张三</name> <age>18</age> <gender>男</gender> </student>
-
现在已经被JSON替代了
{"name":"张三","age":"18","gender":"男"}
1.3 AJAX的特点
优点
- 可以无需刷新页面而与服务器端进行通信。
- 允许你根据用户事件来更新部分页面内容。
缺点
- 没有浏览历史
- 存在跨域问题
- SEO不友好
1.4 HTTP协议
HTTP (hypertext transport protocol)协议『超文本传输协议』﹐协议详细规定了浏览器和万维网服务器之间互相通信的规则。
1.5 请求报文
重点是格式与参数:
格式:
-
行
- 请求类型:GET或POST
- 路径:/s?ie=utf-8
- http协议版本:HTTP/1.1
GET /s?ie=utf-8 HTTP/1.1
-
头
Host:atguigu.com Cookie:name=guigu Content-type:application/ x-wWw-form-urlencoded User-Agent:chrome 83
-
空行
-
体
username=admin&password=admin
1.6 响应报文
-
行
- 协议版本
- 响应状态码
- 响应状态字符串
HTTP/1.1 200 OK
-
头
Content-Type: text/html;charset=utf-8 Content-length: 2048 Content-encoding: gzip
-
空行
-
体
<html> <head></head> <body> <h1>尚硅谷</h1> </body> </html>
二、基本使用
2.1 express服务端框架基本使用
基于 Node.js 平台,快速、开放、极简的 Web 开发框架
终端运行
npm init --yes //初始化
npm i express //安装
创建js文件(例:express.js),写入以下代码:
//1、引入express
const express = require('express');
//创建应用对象
const app = express();
//3、创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的封装
app.get('/',(request,response)=>{
//设置响应
response.send('hello,world!nope! hello,express!')
});
//4、监听端口启动服务
app.listen(8080,()=>{
console.log("服务已经启动,8080端口监听中......")
})
跳转至js所在文件夹,终端运行
node express.js
ctrl c 结束端口
2.2 AJAX发送GET请求
点击按钮发送请求:
请求格式:
xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8000')
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState===4){
if(xhr.status>=200 && xhr.status<300){
result.innerHTML = xhr.response;
}
}
}
在前端GET.html中写如以下代码:
<!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</title>
<style>
#result{
width: 300px;
height: 80px;
border:3px solid red;
line-height:80px;
font-weight: bold;
text-align: center;
}
</style>
</head>
<body>
<button>点我发送请求</button>
<div id="result"></div>
<script>
//获取按钮和文本框元素
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById('result');
//绑定事件
btn.onclick = function(){
//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
//change 改变
xhr.onreadystatechange = function(){
//判断(服务端返回了所有的结果,4)
if(xhr.readyState===4){
//判断响应状态码200、404、403、401、500
//2xx的都是成功
if(xhr.status>=200 && xhr.status<300){
//处理结果:行 头 空行 体
console.log(xhr.status); //响应状态码
console.log(xhr.statusText); //响应状态字符串
console.log(xhr.getAllResponseHeaders()); //所有响应头
console.log(xhr.response); //响应体
//设置div的文本
result.innerHTML = xhr.response;
}
}
}
}
</script>
</body>
</html>
express服务端格式:
const express = require('express');
const app = express();
app.get('/',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('hello')
}
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中......")
})
在服务端service.js中写入以下代码:
//1、引入express
const express = require('express');
//创建应用对象
const app = express();
//3、创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的封装
app.get('/server',(request,response)=>{
//设置响应头 -设置允许跨域
response.setHeader('Access-Control-Allow-Origin','*');
//设置响应体
response.send('hello!我是通过AJAX传过来的!')
});
//4、监听端口启动服务
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中......")
})
启动服务端,启动前端网页,效果如下:
AJAX-GET设置请求参数
- 在地址后,以?分割,a=xx&b=xx&c=xx
http://127.0.0.1:8000/server?a=100&b=20&c=5
2.3 AJAX发送POST请求
与GET相似,将上述GET全改成POST,如:
xhr.open('POST','http://127.0.0.1:8000/server');
app.post('/server',(request,response)=>{
//设置响应头 -设置允许跨域
response.setHeader('Access-Control-Allow-Origin','OPTIONS,GET,POST');
//设置响应体
response.send('hello!我是通过AJAX传过来的!')
});
响应头为:
response.setHeader('Access-Control-Allow-Origin','OPTIONS,GET,POST');
AJAX-POST设置请求参数
POST可以在send()中设置请求参数:
xhr.send('a=1&b=3')
2.4 AJAX设置请求头信息
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
在service.js中
app.all('/service',(request,response)=>{
...
}) //表示能接受任意类型的请求。
2.5 服务端响应JSON数据
//设置响应体
const data = {
name:'邹君',
age:21,
sex:'男'
};
//对对象进行字符串转换
let str = JSON.stringify(data);
response.send(str)
通过上述操作,前端只能获得字符串,在前端接受时,也可以通过以下方法转换为对象。
//手动转换
let data = JSON.parse(xhr.response)
result.innerHTML = data.name;
//自动转换-设置返回数据类型
xhr.responseType = 'json';
...
result.innerHTML = xhr.response.name;
三、
3.1 nodemon自动重启工具
使用node service.js启动服务,最大的弊端就是每次修改js代码,都需要手动关闭再重启服务,特别麻烦,可以通过nodemon自动重启工具来解决:
安装:
先停掉所有服务,然后
npm install -g nodemom
安装完成后,就可以通过以下指令来启动服务端
nodemon service.js
3.2 AJAX请求超时和网络异常处理
//超时设置
xhr.timeout = 2000;
//超时回调
xhr.ontimeout = function(){
alert('请求超时')
}
//异常回调
xhr.onerror = function(){
alert('网络异常')
}
3.3 AJAX取消请求
xhr.abort();
3.4 AJAX请求重复发送问题
为了防止同一个请求重复发送,可以在发送时判断是否存在正在发送的相同请求,有则取消上述请求,发生新请求
//1、创建对象
let xhr = new XMLHttpRequest();
let isSending = false;
btn.onclick = function(){
xhr.responseType = 'json';
//2、初始化,设置请求方法和url
if(isSending){
xhr.abort()
}
xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8000/server-time');
isSending = true;
//3、发送
xhr.send();
//4、事件绑定 处理服务端返回的结果
//on when 当...时候
//readyState xhr对象的属性,表示状态0、1、2、3、4
//change 改变
xhr.onreadystatechange = function(){
//判断(服务端返回了所有的结果,4)
if(xhr.readyState===4){
//判断响应状态码200、404、403、401、500
//2xx的都是成功
isSending = false
if(xhr.status>=200 && xhr.status<300){
//处理结果:行 头 空行 体
console.log(xhr.status); //响应状态码
console.log(xhr.statusText); //响应状态字符串
console.log(xhr.getAllResponseHeaders()); //所有响应头
console.log(xhr.response); //响应体
//设置div的文本
result.innerHTML = xhr.response.name;
}else{
}
}
}
}
3.5 使用fetch函数发送AJAX请求
前端可以使用fetch函数来发送AJAX请求:
格式如下:
fetch('http://127.0.0.1:8000',{
//请求方法
method:'POST',
//请求头
headers:{
name:''
},
//请求体,GET请求不能设置body
body:{
}
}).then(response=>{
console.log(response);
})
四、跨域
4.1 同源策略
同源:协议、域名、端口 必须完全相同。
违背同源策略就是跨域;