ajax
一. 初识前后端交互
传统网站的问题:
-
为了获取数据,需要重新加载,浪费资源,增加等待时间,性能不好
-
验证表单过程中,一项内容不合格,页面需要重新加载,体验不好
解决问题:
-
ajax全名async javascript and XML -
是前后台交互的能力
-
也就是我们客户端给服务端发送消息的工具,以及接受响应的工具
-
是一个 默认异步 执行机制的功能
AJAX 的优势
-
不需要插件的支持,原生 js 就可以使用
-
用户体验好(不需要刷新页面就可以更新数据)
-
减轻服务端和带宽的负担
-
缺点: 搜索引擎的支持度不够,因为数据都不在页面上,搜索引擎搜索不到
-
二. 原生Ajax
AJAX 基础
-
在 js 中有内置的构造函数来创建 ajax 对象
-
创建 ajax 对象以后,我们就使用 ajax 对象的方法去发送请求和接受响应
创建一个 ajax 对象
// IE9及以上
const xhr = new XMLHttpRequest()
// IE9以下
const xhr = new ActiveXObject('Mricosoft.XMLHTTP')
-
上面就是有了一个 ajax 对象
-
我们就可以使用这个
xhr对象来发送 ajax 请求了
配置链接信息
const xhr = new XMLHttpRequest()
// xhr 对象中的 open 方法是来配置请求信息的
// 第一个参数是本次请求的请求方式 get / post / put / ...
// 第二个参数是本次请求的 url
// 第三个参数是本次请求是否异步,默认 true 表示异步,false 表示同步
// xhr.open('请求方式', '请求地址', 是否异步)
xhr.open('get', './data.php')
-
上面的代码执行完毕以后,本次请求的基本配置信息就写完了
发送请求
const xhr = new XMLHttpRequest()
xhr.open('get', './data.php')
// 使用 xhr 对象中的 send 方法来发送请求
xhr.send()
-
上面代码是把配置好信息的 ajax 对象发送到服务端
一个基本的 ajax 请求
-
一个最基本的 ajax 请求就是上面三步
-
但是光有上面的三个步骤,我们确实能把请求发送的到服务端
-
如果服务端正常的话,响应也能回到客户端
-
但是我们拿不到响应
-
如果想拿到响应,我们有两个前提条件
-
本次 HTTP 请求是成功的,也就是我们之前说的 http 状态码为 200 ~ 299
-
ajax 对象也有自己的状态码,用来表示本次 ajax 请求中各个阶段
-
ajax 状态码
-
ajax 状态码 -
xhr.readyState -
是用来表示一个 ajax 请求的全部过程中的某一个状态
-
readyState === 0: 表示未初始化完成,也就是open方法还没有执行 -
readyState === 1: 表示配置信息已经完成,也就是执行完open之后 -
readyState === 2: 表示send方法已经执行完成 -
readyState === 3: 表示正在解析响应内容 -
readyState === 4: 表示响应内容已经解析完毕,可以在客户端使用了
-
-
这个时候我们就会发现,当一个 ajax 请求的全部过程中,只有当
readyState === 4的时候,我们才可以正常使用服务端给我们的数据 -
所以,配合 http 状态码为 200 ~ 299
-
一个 ajax 对象中有一个成员叫做
xhr.status -
这个成员就是记录本次请求的 http 状态码的
-
-
两个条件都满足的时候,才是本次请求正常完成
readyStateChange
-
在 ajax 对象中有一个事件,叫做
readyStateChange事件 -
这个事件是专门用来监听 ajax 对象的
readyState值改变的的行为 -
也就是说只要
readyState的值发生变化了,那么就会触发该事件 -
所以我们就在这个事件中来监听 ajax 的
readyState是不是到 4 了const xhr = new XMLHttpRequest() xhr.open('get', './data.php') xhr.send() xhr.onreadyStateChange = function () { // 每次 readyState 改变的时候都会触发该事件 // 我们就在这里判断 readyState 的值是不是到 4 // 并且 http 的状态码是不是 200 ~ 299 if (xhr.readyState === 4 && /^2\d{2}$/.test(xhr.status)) { // 这里表示验证通过 // 我们就可以获取服务端给我们响应的内容了 } }
responseText
-
ajax 对象中的
responseText成员 -
就是用来记录服务端给我们的响应体内容的
-
所以我们就用这个成员来获取响应体内容就可以
const xhr = new XMLHttpRequest() xhr.open('get', './data.php') xhr.send() xhr.onreadyStateChange = function () { if (xhr.readyState === 4 && /^2\d{2}$/.test(xhr.status)) { // 我们在这里直接打印 xhr.responseText 来查看服务端给我们返回的内容 console.log(xhr.responseText) } }
使用 ajax 发送请求时携带参数
-
我们使用 ajax 发送请求也是可以携带参数的
-
参数就是和后台交互的时候给他的一些信息
-
但是携带参数 get 和 post 两个方式还是有区别的
发送一个带有参数的 get 请求
-
get 请求的参数就直接在 url 后面进行拼接就可以
const xhr = new XMLHttpRequest() xhr.open('get', './data.php?a=100&b=200') xhr.send()-
这样服务端就能接受到两个参数
-
一个是 a,值是 100
-
一个是 b,值是 200
-
发送一个带有参数的 post 请求
-
post 请求的参数是携带在请求体中的,所以不需要再 url 后面拼接
const xhr = new XMLHttpRequest() xhr.open('get', './data.php') /* 如果是用 ajax 对象发送 post 请求,必须要先设置一下请求头中的 content-type */ xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded') xhr.send('a=100&b=200')-
application/x-www-form-urlencoded表示的数据格式就是key=value&key=value
-
不同的请求方式
-
get 偏向获取
-
post 偏向提交
-
put 偏向更新
-
patch 偏向修改部分
-
delete 偏向删除信息
-
head 偏向获取服务器头的信息
-
option 偏向获取服务器设备信息
-
connnect 保留请求方式
三. Fetch
XMLHttpRequest 是一个设计粗糙的 API,配置和调用方式非常混乱, 而且基于事件的异步模型写起来不友好。
兼容性不好 polyfill: https://github.com/camsong/fetch-ie8
1.用法
fetch("http://localhost:3000/users")
.then(res=>res.json())
.then(res=>{
console.log(res)
})
fetch("http://localhost:3000/users",{
method:"POST",
headers:{
"content-type":"application/json"
},
body:JSON.stringify({
username:"kerwin",
password:"123"
})
})
.then(res=>res.json())
.then(res=>{
console.log(res)
})
fetch("http://localhost:3000/users/5",{
method:"PUT",
headers:{
"content-type":"application/json"
},
body:JSON.stringify({
username:"kerwin",
password:"456"
})
})
.then(res=>res.json())
.then(res=>{
console.log(res)
})
fetch("http://localhost:3000/users/5",{
method:"DELETE"
})
.then(res=>res.json())
.then(res=>{
console.log(res)
})
2.错误处理
fetch("http://localhost:3000/users1")
.then(res=>{
if(res.ok){
return res.json()
}else{
return Promise.reject({
status:res.status,
statusText:res.statusText
})
}
})
.then(res=>{
console.log(res)
})
.catch(err=>{
console.log(err)
})
四. axios
Axios是一个基于promise 的 HTTP 库,可以用在浏览器和 node.js中。
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
1. get请求
axios.get("http://localhost:3000/users",{
params:{
name:"kerwin"
}
}).then(res=>{
console.log(res.data)
})
2.post请求
axios.post("http://localhost:3000/users",{
name:"kerwin",
age:100
}).then(res=>{
console.log(res.data)
})
3. put请求
axios.put("http://localhost:3000/users/12",{
name:"kerwin111",
age:200
}).then(res=>{
console.log(res.data)
})
4. delete请求
axios.delete("http://localhost:3000/users/11").then(res=>{
console.log(res.data)
})
5. axios(config)配置
axios({
method: 'post',
url: 'http://localhost:3000/users',
data: {
name: 'kerwin',
age: 100
}
})
.then(res => {
console.log(res.data)
}).catch(err=>{
console.log(err)
})
6. axios拦截器
axios.interceptors.request.use(function (config) {
// Do something before request is sent
console.log("loading-开始")
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
});
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
console.log("loading-结束")
return response;
}, function (error) {
// Any status codes that falls outside the range of 2xx cause this function to trigger
// Do something with response error
console.log("loading---结束")
return Promise.reject(error);
});
7. axios 中断器
const controller = new AbortController();
axios.get('/foo/bar', {
signal: controller.signal
}).then(function(response) {
//...
});
// cancel the request
controller.abort()
五. 同源策略(Same-origin policy)
一个 URL 有三部分组成:协议、域名(指向主机)、端口,只有这三个完全相同的 URL 才能称之为同源。如下,能和 http://www.example.com/dir1/index.html 同源的是?
| URL | 结果 | 原因 |
|---|---|---|
http://www.example.com/dir2/api | 同源 | 只有路径不同 |
https://www.example.com/api | 不同源 | 协议不同 |
http://www.example.com:81/dir1/etc.html | 不同源 | 端口不同 ( http:// 默认端口是80) |
http://www.kerwin.com/dir1/other.html | 不同源 | 域名不同 |
(1) 无法读取非同源网页的 Cookie、LocalStorage 。 (2) 无法接触非同源网页的 DOM。 (3) 无法向非同源地址发送 AJAX 请求(可以发送,但浏览器会拒绝接受响应)。
注意:同源策略是浏览器的行为,是为了保护本地数据不被JavaScript代码获取回来的数据污染,因此拦截的是客户端发出的请求回来的数据接收,即请求发送了,服务器响应了,但是无法被浏览器接收。
六. jsonp
Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据。
为什么我们从不同的域(网站)访问数据需要一个特殊的技术( JSONP )呢?这是因为同源策略。
const script = document.createElement('script')
script.src = './kerwin.txt'
document.body.appendChild(script)
mysearch.oninput = function(evt){
console.log(evt.target.value)
if(!evt.target.value){
list.innerHTML = ""
return
}
var oscript = document.createElement("script")
oscript.src = `https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=36542,36464,36673,36454,31660,36692,36166,36695,36697,36570,36074,36655,36345,26350,36469,36314&wd=${evt.target.value}&req=2&csor=1&cb=test&_=1656294200527`
document.body.appendChild(oscript)
oscript.onload = function(){
oscript.remove()
}
}
function test(obj){
console.log(obj.g)
list.innerHTML = obj.g.map(item=>
`<li>${item.q}</li>`
).join("")
}
2.axios
axios是一个基于Promise的HTTP库,可以用在浏览器和node.js中
http://www.axios-js.com/zh-cn/docs/index.html#axios
特性
-
从浏览器中创建 XMLHttpRequsts
-
从node.js 创建Http请求
-
支持 Promise API
-
拦截请求与响应
-
转换请求数据和响应数据
-
取消请求
-
自动转换 JSON数据
-
客户端支持防御 XSRF
安装
使用 npm :
$ npm install axios
使用 bower :
$ bower install axios
使用 cdn:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
执行GET请求
axios.get(' /user?ID=123456 ')
.then( function( response){ console.log(response) }
.catch( function( error ){ console.log(error) }
)
等同
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
执行POST请求
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
多个并发请求
axios.all([ axios.get(URL),axios.get(URL) ])
.then(res=>{
console.log(res)
}).catch(err=>{
console.log(err)
})
//通过这个方式会将两个接口响应的返回数据存入到一个长度为2的数组内
等同
axios.all([ axios.get(URL),axios.get(URL) ])
.then(
axios.spread(( res1,res2 )=>{
console.log(res1)
console.log(res2)
})
).catch(err=>{
console.log(err)
})
助手函数
//进行一个并发请求 axios.all() //对所得参数进行分割 axios.spread()
全局配置
//设置默认的全局地址 axios.defaults.baseURL = 'http://locahost:8080/' //设置超时时间 axios.defaults.timeout = 5000
axios.get("URL").then(e=>{ ... }).catch( err =>{ ... } )
创建实例
可以使用自定义配置创建一个axios实例
axios.create([config])
//将实例封装方便应对多个不同的网络请求
let temp = axios.create({
baseURL:"http://locahost:8080/",
timeout:5000
})
temp({
url:'URL'
}).then( item =>{ ... } )
拦截器
axios为提供了两种拦截器模式,一种是请求拦截、一种是响应拦截。
拦截器作用是为了在发起请求或响应时做出相应的操作
请求拦截器
axios.interceptors.request.use(config=>{
console.log("进入拦截器")
console.log(config)
//拦截后需要放行
return config;
},err=>{
console.log("请求失败")
console.log(err)
})
axios.get(URL).then(res=>{ ... })
响应拦截器
axios.interceptors.response.use(config=>{
console.log("进入响应拦截器")
console.log(config)
//拦截后需要放行
return config;
},err=>{
console.log("响应失败")
console.log(err)
})
axios.get(URL).then(res=>{ ... })
2523

被折叠的 条评论
为什么被折叠?



