【初始前后端交互+原生Ajax+Fetch+axios+同源策略+解决跨域】

初始前后端交互+原生Ajax+Fetch+axios+同源策略+解决跨域

  • 1 初识前后端交互
  • 2 原生Ajax
    • 2.1 Ajax基础
    • 2.2 Ajax案例
    • 2.3 ajax请求方式
  • 3 Fetch
    • 3.1 fetch基础
    • 3.2 fetch案例
  • 4 axios
    • 4.1 axios基础
    • 4.2 axios使用
      • 4.2.1 axios拦截器
      • 4.2.2 axios中断器
  • 5 同源策略
  • 6 解决跨域
    • 6.1 jsonp
    • 6.2 其他技术手段

1 初识前后端交互

  • 传统网站的问题:
    1> 为了获取数据,需要重新加载,浪费资源,增加等待时间,性能不好。
    2> 验证表单过程中,一项内容不合格,页面需要重新加载,体验不好。
  • 解决问题:
    1> ajax 全名 async javascript and XML
    2> 是前后台交互的能力
    3> 也就是我们客户端给服务端发送消息的工具,以及接受响应的工具
    4> 是一个默认异步执行机制的功能
  • AJAX的优势:
    1> 不需要插件的支持,原生 js 就可以使用
    2> 用户体验好(不需要刷新页面就可以更新数据)
    3> 减轻服务端和带宽的负担
    4> 缺点: 搜索引擎的支持度不够,因为数据都不在页面上,搜索引擎搜索不到

2 原生Ajax

2.1 Ajax基础

  • 在 js 中有内置的构造函数来创建 ajax 对象;
const xhr = new XMLHttpRequest()
// 上面就是有了一个 ajax 对象
// 我们就可以使用这个 `xhr` 对象来发送 ajax 请求了
  • 配置链接信息;
xhr.open("get","1.json ",true)
// xhr.open('请求方式', '请求地址', 是否异步)
// xhr 对象中的 open 方法是来配置请求信息的
// 第一个参数是本次请求的请求方式 get / post / put / ...
// 第二个参数是本次请求的 url
// 第三个参数是本次请求是否异步,默认 true 表示异步,false 表示同步
  • 创建 ajax 对象以后,我们就使用 ajax 对象的方法去发送请求和接受响应。
xhr.send()
// 使用 xhr 对象中的 send 方法来发送请求
// 上面代码是把配置好信息的 ajax 对象发送到服务端
一、一个基本的ajax请求:
	一个最基本的 ajax 请求就是上面三步
	但是光有上面的三个步骤,我们确实能把请求发送的到服务端
	如果服务端正常的话,响应也能回到客户端,但是我们拿不到响应
	如果想拿到响应,我们有两个前提条件:
		1. 本次 HTTP 请求是成功的,也就是我们之前说的 http 状态码为 200 ~ 299
		2. 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 了
四、responseText
	ajax 对象中的 `responseText` 成员
	就是用来记录服务端给我们的响应体内容的
	所以我们就用这个成员来获取响应体内容就可以
<!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>aaa</button>
    <script>
        var xhr = new XMLHttpRequest()

        xhr.open("get","1.json ",true)
        // 第一个参数 get post 请求方式
        // 第二个参数 请求地址
        // 第三个参数 是否异步

        xhr.send()

        // 监听
        // 方法一
        /* xhr.onreadystatechange = function() {
            console.log("电话接通");
            console.log(xhr.readyState); // 准备状态
            if(xhr.readyState === 4) {
                // if(xhr.status === 200) {
                if(/^2\d{2}$/.test(xhr.status)) {
                    console.log(JSON.parse(xhr.responseText))
                } else {
                    console.log("error",xhr.responseText);
                }
            }
        } */

        // 方法二
        xhr.onload = function() {
            if(/^2\d{2}$/.test(xhr.status)) {
                console.log(JSON.parse(xhr.responseText))
            } else {
                console.log("error",xhr.responseText);
            }
        }
    </script>
</body>
</html>
{
    "name":"xiaowang"
}

在这里插入图片描述

2.2 Ajax案例

<!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 id="btn">click</button>
    <ul id="list">
        
    </ul>
    <script>
        var obtn = document.querySelector('#btn')
        var olist = document.querySelector('#list')
        obtn.onclick = function() {
            var xhr = new XMLHttpRequest()

            xhr.open("get","http://www.xiongmaoyouxuan.com/api/tabs", true) // 后端接口地址

            xhr.send()

            xhr.onload = function() {
                if(/^2\d{2}$/.test(xhr.status)) {
                    // console.log(JSON.parse(xhr.responseText))
                    render(JSON.parse(xhr.responseText))
                } else {
                    console.log("error",xhr.responseText);
                }
            }
        }
        // 渲染页面
        function render(res) {
            console.log(res.data.list);

            var newlist = res.data.list.map(function(item){
                return `<li>
                    <img src="${item.imageUrl}"/>
                    <div>${item.name}</div>    
                </li>`
            })

            console.log(newlist);

            olist.innerHTML = newlist.JSON.join("")
        }
    </script>
</body>
</html>
{
    "data": {
        "list": [
            {
                "name": "假数据111",
                "imageUrl": "http://img1.lukou.com/static/p/fb/tab/1/20181211-151644.jpeg"
            },
            {
                "name": "假数据222",
                "imageUrl": "http://img1.lukou.com/static/p/fb/tab/1/20181211-151644.jpeg"
            },
            {
                "name": "假数据111",
                "imageUrl": "http://img1.lukou.com/static/p/fb/tab/1/20181211-151644.jpeg"
            },
            {
                "name": "假数据111",
                "imageUrl": "http://img1.lukou.com/static/p/fb/tab/1/20181211-151644.jpeg"
            }

        ]
    }
}

2.3 ajax请求方式

  • 不同的请求方式:
    1> get 偏向获取
    2> post 偏向提交
    3> put 偏向更新
    4> patch 偏向修改部分
    5> delete 偏向删除信息
    6> head 偏向获取服务器头的信息
    7> option 偏向获取服务器设备信息
    8> connect 保留请求方式

  • 使用get请求方式(不传参数):

oget.onclick = function() {
	var xhr = new XMLHttpRequest()
    xhr.open("GET","http://localhost:3000/list", true) // 后端接口地址
    xhr.send()
    xhr.onload = function() {
    	if(/^2\d{2}$/.test(xhr.status)) {
        	console.log(JSON.parse(xhr.responseText))
            // render(JSON.parse(xhr.responseText))
		} else {
        	console.log("error",xhr.responseText);
        }
	}
}
  • 使用get请求方式(传参数):
oget.onclick = function() {
	var xhr = new XMLHttpRequest()
    xhr.open("GET","http://localhost:3000/users?id=1", true)
    xhr.send()
    xhr.onload = function() {
    	if(/^2\d{2}$/.test(xhr.status)) {
        	console.log(JSON.parse(xhr.responseText))
            // render(JSON.parse(xhr.responseText))
		} else {
        	console.log("error",xhr.responseText);
        }
	}
}
  • 使用post请求方式(传参数):
opost.onclick = function() {
	var xhr = new XMLHttpRequest()
    xhr.open("POST","http://localhost:3000/users", true) // 后端接口地址
    // 两种提交方式:
    // form编码 name=kerwin&age=100
    // json {name:"kerwin",age:100}
    // 方式一
    /* xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
    xhr.send(`name=tiechui&age=18`) */ // 数据放在这里
    // 方式二
    xhr.setRequestHeader("content-type","application/json")
    xhr.send(JSON.stringify({
    	name:"guludunzi",
        age:90
    })) // 数据放在这里
    xhr.onload = function() {
        if(/^2\d{2}$/.test(xhr.status)) {
        	console.log(JSON.parse(xhr.responseText))
            // render(JSON.parse(xhr.responseText))
        } else {
            console.log("error",xhr.responseText);
        }
	}
}
  • 使用put请求方式(传参数):
oput.onclick = function() {
    var xhr = new XMLHttpRequest()
    xhr.open("PUT","http://localhost:3000/users/4", true) // 后端接口地址
    // 两种提交方式:
    // form编码 name=kerwin&age=100
    // json {name:"kerwin",age:100}
    // 方式一
    /* xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
    xhr.send(`name=tiechui&age=18`) */ // 数据放在这里
    // 方式二
    xhr.setRequestHeader("content-type","application/json")
    xhr.send(JSON.stringify({
        username:"guludunzi",
        age:70
    })) // 数据放在这里
    xhr.onload = function() {
        if(/^2\d{2}$/.test(xhr.status)) {
            console.log(JSON.parse(xhr.responseText))
            // render(JSON.parse(xhr.responseText))
        } else {
            console.log("error",xhr.responseText);
        }
    }
}
  • 使用patch请求方式(传参数):
opatch.onclick = function() {
    var xhr = new XMLHttpRequest()
    xhr.open("PATCH","http://localhost:3000/users/4", true) // 后端接口地址
    // 两种提交方式:
    // form编码 name=kerwin&age=100
    // json {name:"kerwin",age:100}
    // 方式一
    /* xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
    xhr.send(`name=tiechui&age=18`) */ // 数据放在这里
    // 方式二
    xhr.setRequestHeader("content-type","application/json")
    xhr.send(JSON.stringify({
        // username:"guludunzi",
        age:180
    })) // 数据放在这里
    xhr.onload = function() {
        if(/^2\d{2}$/.test(xhr.status)) {
            console.log(JSON.parse(xhr.responseText))
            // render(JSON.parse(xhr.responseText))
        } else {
            console.log("error",xhr.responseText);
        }
    }
}
  • 使用delete请求方式(无参数):
odelete.onclick = function() {
    var xhr = new XMLHttpRequest()
    xhr.open("DELETE","http://localhost:3000/users/4", true) // 后端接口地址
    xhr.send()
    xhr.onload = function() {
        if(/^2\d{2}$/.test(xhr.status)) {
            console.log(JSON.parse(xhr.responseText))
            // render(JSON.parse(xhr.responseText))
        } else {
            console.log("error",xhr.responseText);
        }
    }
}

3 Fetch

  • XMLHttpRequest 是一个设计粗糙的 API,配置和调用方式非常混乱, 而且基于事件的异步模型写起来不友好。
  • 先在集成终端中打开json-server监视:
    在这里插入图片描述

3.1 fetch基础

  • 使用get请求方式(不传参数):
oget.onclick = function() {
    fetch("http://localhost:3000/users")
    .then(res =>res.json())
    .then(res => {
        console.log(res);
    })
}
  • 使用get请求方式(传参数):
oget.onclick = function() {
    fetch("http://localhost:3000/users?id=1")
    .then(res =>res.json())
    .then(res => {
        console.log(res);
    })
}
  • 使用post请求方式(传参数):
opost.onclick = function() {
    fetch("http://localhost:3000/users",{
        method:"post",
        headers:{
            // 两种提交方式:
            // form编码 name=kerwin&age=100
            // json {name:"kerwin",age:100}
            // 方式一
            // "content-type":"application/x-www-form-urlencoded"
            // 方式二
            "content-type":"application/json"
        },
        // body:"name=dazhuang&age=100" // 方式一body
        body:JSON.stringify({
            name:"xiaoli",
            age: 20
        })
    })
    .then(res =>res.json())
    .then(res => {
        console.log(res);
    })
}
  • 使用put请求方式(传参数):
oput.onclick = function() {
    fetch("http://localhost:3000/users/2",{
        method:"put",
        headers:{
            // 两种提交方式:
            // form编码 name=kerwin&age=100
            // json {name:"kerwin",age:100}
            // 方式一
            // "content-type":"application/x-www-form-urlencoded"
            // 方式二
            "content-type":"application/json"
        },
        // body:"name=dazhuang&age=100" // 方式一body
        body:JSON.stringify({
            name:"xiaowang",
            age: 76
        })
    })
    .then(res =>res.json())
    .then(res => {
        console.log(res);
    })
}
  • 使用patch请求方式(传参数):
opatch.onclick = function() {
    fetch("http://localhost:3000/users/2",{
        method:"PATCH",
        headers:{
            // 两种提交方式:
            // form编码 name=kerwin&age=100
            // json {name:"kerwin",age:100}
            // 方式一
            // "content-type":"application/x-www-form-urlencoded"
            // 方式二
            "content-type":"application/json"
        },
        // body:"name=dazhuang&age=100" // 方式一body
        body:JSON.stringify({
            age: 33
        })
    })
    .then(res =>res.json())
    .then(res => {
        console.log(res);
    })
}
  • 使用delete请求方式(无参数):
odelete.onclick = function() {
    fetch("http://localhost:3000/users/2",{
        method:"delete",
    })
    .then(res =>res.json())
    .then(res => {
        console.log(res);
    })
}
  • 以使用get请求方式为例失败处理:
oget.onclick = function() {
    fetch("http://localhost:3000/users?id=1")
    .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", err)
	})
}

在这里插入图片描述

3.2 fetch案例

  • 通过输入作者姓名找到新闻标题,再根据新闻标题对应的新闻id找到所对应的评论内容。
  • db.json代码:
{
    "news": [
        { "id" : 1, "title" : "男人看了沉默,女人看了流泪", "author" : "kerwin"},
        { "id" : 2, "title" : "震惊!他年薪仅1元", "author" : "tiechui"},
        { "id" : 3, "title" : "速看!万分危急!", "author" : "gangdan"}
    ],
    "comments": [
        { "id" : 1, "body" : "我是男人", "newsId" : 1 },
        { "id" : 2, "body" : "我是女人", "newsId" : 1 },
        { "id" : 3, "body" : "我年薪2元", "newsId" : 2 },
        { "id" : 4, "body" : "我年薪3元", "newsId" : 2 },
        { "id" : 5, "body" : "1块钱就能买1块钱的东西", "newsId" : 3 },
        { "id" : 6, "body" : "2块钱就能买2块钱的东西", "newsId" : 3 }
    ]
}
  • 基于fetch的01.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>Document</title>
</head>
<body>
    <input type="text" id="search">
    <h1 id="title"></h1>
    <ul id="list"></ul>
    <script>
        var osearch = document.querySelector('#search')
        var otitle = document.querySelector('#title')
        var olist = document.querySelector('#list')

        osearch.oninput = function() {
            fetch(`http://localhost:3000/news?author=${osearch.value}`)
            .then( res => res.json())
            .then( res => {
                if(res.length > 0) {
                    otitle.innerHTML = res[0].title
                    return fetch(`http://localhost:3000/comments?newsId=${res[0].id}`)
                    .then(res=>res.json())
                }else {
                    otitle.innerHTML = ""
                    return res
                }
            }).then(res=>{
                console.log(res)
                olist.innerHTML = res.map( item => `
                    <li>
                        ${item.body}
                    </li>
                `).join("") // join是为了避免中间有逗号
            })
        }
    </script>
</body>
</html>
  • fetch结合async await下的01.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>Document</title>
</head>
<body>
    <input type="text" id="search">
    <h1 id="title"></h1>
    <ul id="list"></ul>
    <script>
        var osearch = document.querySelector('#search')
        var otitle = document.querySelector('#title')
        var olist = document.querySelector('#list')

        osearch.oninput = async function() {
            var res = await fetch(`http://localhost:3000/news?author=${osearch.value}`)
            .then( res => res.json())
            var result
            if(res.length > 0) {
                otitle.innerHTML = res[0].title
                result = await fetch(`http://localhost:3000/comments?newsId=${res[0].id}`)
                .then(res=>res.json())
            }else {
                otitle.innerHTML = ""
                result = res
            }
            // console.log("111",result)
            olist.innerHTML = result.map( item => `
                <li>
                    ${item.body}
                </li>
            `).join("") // join是为了避免中间有逗号
        }
    </script>
</body>
</html>

在这里插入图片描述

4 axios

  • Axios是一个基于promise 的 HTTP 库,可以用在浏览器和 node.js中。
  • https://www.npmjs.com/package/axios

4.1 axios基础

  • 使用get请求方式(不传参数):
oget.onclick = function() {
	axios.get("http://localhost:3000/users")
    // 若成功走.then
    .then(res => {
    	console.log(res.data)
     })
     // 若失败走.catch
     .catch(err => {
        console.log("err",err)
     })
}
  • 使用get请求方式(传参数):
oget.onclick = function() {
	axios.get("http://localhost:3000/users?name=kerwin")
    // 若成功走.then
    .then(res => {
    	console.log(res.data)
     })
     // 若失败走.catch
     .catch(err => {
        console.log("err",err)
     })
}
  • 使用get请求方式(传参数且为对象结构):
oget.onclick = function() {
	axios.get("http://localhost:3000/users",{
		params:{
			name:"kerwin"
		}
	})
    // 若成功走.then
    .then(res => {
    	console.log(res.data)
     })
     // 若失败走.catch
     .catch(err => {
        console.log("err",err)
     })
}
  • 使用post请求方式(传参数):
 opost.onclick = function() {
	// 以json方式传参数
	/* axios.post("http://localhost:3000/users",{
    	name:"xiaoming",
        age:18
    }) */
    
    // 以form方式传参数方法一
    // axios.post("http://localhost:3000/users","name=tiechui&age=77")

	// 以form方式传参数方法二
	const params = new URLSearchParams({name:"dazhuang",age:13});
    axios.post("http://localhost:3000/users",params)

	// 若成功走.then
    .then(res => {
    	console.log(res.data)
    })
    // 若失败走.catch
    .catch(err => {
    	console.log("err",err)
    })
}
  • 使用put请求方式(传参数):
oput.onclick = function() {
	axios.put("http://localhost:3000/users/4",{
    	name:"dazhuang",
        age:99
    })
    // 若成功走.then
    .then(res => {
    	console.log(res.data)
    })
    // 若失败走.catch
    .catch(err => {
    	console.log("err",err)
    })
}
  • 使用patch请求方式(传参数):
opatch.onclick = function() {
	axios.patch("http://localhost:3000/users/4",{
        age:101
    })
    // 若成功走.then
    .then(res => {
    	console.log(res.data)
    })
    // 若失败走.catch
    .catch(err => {
    	console.log("err",err)
    })
}
  • 使用delete请求方式(无参数):
odelete.onclick = function() {
	axios.delete("http://localhost:3000/users/4")
    // 若成功走.then
    .then(res => {
    	console.log(res.data)
	})
    // 若失败走.catch
    .catch(err => {
        console.log("err",err)
    })
}
  • axios(config)配置:
axios({
	method:"post",
	url:'http://localhost:3000/users',
	// put post等放入data中 get放入params中
	data:{
		name:'kerwin',
		age: 88
	}
})
.then(res => {
	console.log(res.data)
})
.catch(err => {
	console.log(err)
})

4.2 axios使用

4.2.1 axios拦截器

  • db.json代码:
{
  "news": [
      { "id" : 1, "title" : "男人看了沉默,女人看了流泪", "author" : "kerwin"},
      { "id" : 2, "title" : "震惊!他年薪仅1元", "author" : "tiechui"},
      { "id" : 3, "title" : "速看!万分危急!", "author" : "gangdan"}
  ],
  "comments": [
      { "id" : 1, "body" : "我是男人", "newsId" : 1 },
      { "id" : 2, "body" : "我是女人", "newsId" : 1 },
      { "id" : 3, "body" : "我年薪2元", "newsId" : 2 },
      { "id" : 4, "body" : "我年薪3元", "newsId" : 2 },
      { "id" : 5, "body" : "1块钱就能买1块钱的东西", "newsId" : 3 },
      { "id" : 6, "body" : "2块钱就能买2块钱的东西", "newsId" : 3 }
  ]
}
  • 01.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>Document</title>
    <!-- 引入axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios@1.1.2/dist/axios.min.js"></script>
</head>
<body>
    <button id="get">get</button>
    <script>
        var oget = document.querySelector("#get")

        // axios拦截器
        // request请求拦截器
        axios.interceptors.request.use(function (config) {
            // Do something before request is sent
            // console.log("loading-开始")
            console.log("loading显示....")
            return config;
        }, function (error) {
            // Do something with request error
            return Promise.reject(error);
        });

        // response响应拦截器
        // 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-结束")
            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---结束")
            console.log("失败-隐藏loading....")
            return Promise.reject(error);
        });

        oget.onclick = function() {
            axios.get('http://localhost:3000/news').then(res => {
                console.log(res.data);
            }).catch(err => {
                console.log("err",err);
            })
        }
    </script>
</body>
</html>

在这里插入图片描述
在这里插入图片描述

4.2.2 axios中断器

const controller = new AbortController();

axios.get('/foo/bar', {
   signal: controller.signal
}).then(function(response) {
   //...
});
// cancel the request
controller.abort()
  • 01.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>Document</title>
    <!-- 引入axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios@1.1.2/dist/axios.min.js"></script>
</head>
<body>
    <button id="get">get</button>
    <!-- 终止按钮 -->
    <button id="abort">abort</button>
    <script>
        var oget = document.querySelector("#get")
        var oabort = document.querySelector("#abort")

        // axios拦截器
        // request请求拦截器
        axios.interceptors.request.use(function (config) {
            // Do something before request is sent
            // console.log("loading-开始")
            console.log("loading显示....")
            return config;
        }, function (error) {
            // Do something with request error
            return Promise.reject(error);
        });

        // response响应拦截器
        // 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-结束")
            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---结束")
            console.log("失败-隐藏loading....")
            return Promise.reject(error);
        });

        // axios中断器
        const controller = new AbortController();

        oget.onclick = function() {
            axios.get('http://localhost:3000/news',{
                signal: controller.signal
            })
            .then(res => {
                console.log(res.data);
            }).catch(err => {
                console.log("err",err);
            })
        }

        oabort.onclick = function() {
            controller.abort()
        }
    </script>
</body>
</html>

在这里插入图片描述

5 同源策略

  • 一个URL有三部分组成:协议、域名(指向主机)、端口,只有这三个完全相同的 URL 才能称之为同源。
  • 如下,能和http://www.example.com/dir1/index.html 同源的是?
    在这里插入图片描述
  • 无法读取非同源网页的 Cookie、LocalStorage 。
  • 无法接触非同源网页的 DOM。
  • 无法向非同源地址发送 AJAX 请求(可以发送,但浏览器会拒绝接受响应)。
  • 注意:同源策略是浏览器的行为,是为了保护本地数据不被JavaScript代码获取回来的数据污染,因此拦截的是客户端发出的请求回来的数据接收,即请求发送了,服务器响应了,但是无法被浏览器接收。

6 解决跨域

6.1 jsonp

  • Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据。
  • 为什么我们从不同的域(网站)访问数据需要一个特殊的技术( JSONP )呢?这是因为同源策略。
  • 1.txt代码:
test("xiaowang")
  • 01.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>Document</title>
</head>
<body>
    <script>
        function test(data){
            console.log("111",data);
        }


        var oscript = document.createElement("script")
        oscript.src = "1.txt"
        document.body.appendChild(oscript)

        // 1.script标签没有跨域限制
        // 2.后端配合返回的是 函数() 调用的方式
        // 3.前端必须提前声明好这个函数

        // 缺点:jsonp只能get请求 无法post、put、delete
    </script>
    <!-- <script>
        // <script src="1.txt">
        // test("xiaowang")
    </script> -->
</body>
</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>Document</title>
</head>
<body>
    <input type="text" id="search">
    <ul id="list">

    </ul>

    <script>
        var osearch = document.querySelector('#search')
        var olist = document.querySelector('#list')

        osearch.oninput = function(){
            // console.log(osearch.value)
            if(osearch.value === "") {
                olist.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=39646,39671,39663,39676,39678,39713,39791,39787,39704,39793,39681,39662&wd=${osearch.value}&req=2&csor=1&cb=test&_=1700639132336`
            document.body.appendChild(oscript)

            oscript.onload = function() {
                oscript.remove() // 创建完script标签 等它load完了 就给它移掉
            }
        }

        function test(data) {
            // console.log(data.g);
            olist.innerHTML = data.g.map(item => 
                `
                    <li>${item.q}</li>
                `
            ).join("")
        }
    </script>
</body>
</html>

在这里插入图片描述

6.2 其他技术手段

  • 加上响应头
  • 反向代理nginx跨域解决方案

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/186104.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

线程-Thread类及常见方法

目录 一、创建线程 1.继承 Thread 类 2. 实现 Runnable 接口 3.匿名内部类创建 Thread 子类对象 4. 匿名内部类创建 Runnable 子类对象 5. lambda 表达式创建 Runnable 子类对象 二、Thread 类及常见方法 2.1 Thread 的常见构造方法 2.2 Thread 的几个常见属性 2.3 启…

防止应用程序截屏(容器式,防止极域电子教室和录屏软件录制)

核心原理、实现目的 1、使用Panel容器将外部窗口嵌入自己写的程序 2、使用防止截屏的函数来对窗口透明&#xff0c;这可以使本窗口内所有窗口在录屏软件上消失 3、解放&#xff0c;抓取&#xff0c;存储句柄&#xff0c;实现摆脱录屏&#xff08;极域监控&#xff09; 程序…

Error querying database. Cause: java.lang.reflect.InaccessibleObjectException:

最近开发过程中&#xff0c;居然碰到了一个Arrays.asList的错&#xff0c;怎么个场景呢&#xff1f;传参一个用固定符号拼接的字符串&#xff0c;需要转成集合然后再myBatis里in判断。然后就报错了。 一、代码层面 service层面&#xff1a; shortDetailUrlList Arrays.asLi…

2022年03月 Scratch(三级)真题解析#中国电子学会#全国青少年软件编程等级考试

Scratch等级考试(1~4级)全部真题・点这里 一、单选题(共25题,每题2分,共50分) 第1题 以下四个选项中,运行哪个积木块,可能得到523这个数值? A: B: C: D: 答案:B 四个选项都遵循统一的公式:随机数ⅹ10+3=523,因此可以得出随

汽车电子 -- 根据DBC解析CAN报文

采集的CAN报文&#xff0c;怎么通过DBC解析呢&#xff1f;有一下几种方法。 首先需要确认是CAN2.0 还是CAN FD报文。 还有是 实时解析 和 采集数据 进行解析。 一、CAN2.0报文实时解析&#xff1a; 1、CANTest工具 使用CAN分析仪 CANalyst-II&#xff0c;采集CAN报文。 使用…

免费获取GPT-4的五种工具

不可否认&#xff0c;由OpenAI带来的GPT-4已是全球最受欢迎的、功能最强大的大语言模型&#xff08;LLM&#xff09;之一。大多数人都需要使用ChatGPT Plus的订阅服务去访问GPT-4。为此&#xff0c;他们通常需要每月支付20美元。那么问题来了&#xff0c;如果您不想每月有这笔支…

导入PIL时报错

在导入PIL时,报以下错误: 查找原因 参考博客 Could not find a version that satisfies the requirement PIL (from versions: ) No matching distributi-CSDN博客,按照wheel后,安装PIL时,报如下的错误。 查找说是python版本与wheel文件版本不同,确认本机python版本 …

Git仓库瘦身大作战:133M 到 4M 的实战

开局两张图 瘦身前瘦身后 目录 开局两张图前言下载 BFG克隆代码Git 仓库瘦身清理存储库储存库 GC推送仓库 Git 瘦身验证结语开源项目 前言 在进行项目开发的过程中&#xff0c;代码仓库的体积可能会逐渐增大&#xff0c;特别是在版本控制系统中保留了大量的历史提交记录和不必…

【经典小练习】修改文件中的数据

文章目录 &#x1f339;例子&#x1f33a;思路&#x1f6f8;方法一✨报错解决 &#x1f6f8;方法二 &#x1f339;例子 文本文件中有下面的数据 2-1-9-4-7-8 将文件中的数据进行排序&#xff0c;变成下面的数据 1-2-4-7-8-9 &#x1f33a;思路 要对这些数据进行排序&#xf…

堆的实现(堆的插入、堆的删除等)超级全

堆的实现&#xff08;堆的插入、堆的删除等&#xff09;超级全 文章目录 堆的实现&#xff08;堆的插入、堆的删除等&#xff09;超级全一、前期基础知识1.树结构①树的定义②树的相关概念③二叉树④满二叉树和完全二叉树a.满二叉树b.完全二叉树 ⑤二叉树的性质⑥二叉树顺序结构…

【JAVA】SpringBoot + mongodb 分页、排序、动态多条件查询及事务处理

【JAVA】SpringBoot mongodb 分页、排序、动态多条件查询及事务处理 1.引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mongodb ↓ -->&…

多功能智能灯杆主要功能有哪些?

多功能智能灯杆这个词相信大家都不陌生&#xff0c;最近几年多功能智能灯杆行业发展迅速&#xff0c;迅速取代了传统路灯&#xff0c;那么多功能智能灯杆相比传统照明路灯好在哪里呢&#xff0c;为什么大家都选择使用叁仟智慧多功能智能灯杆呢&#xff1f;所谓多功能智能灯杆着…

如何开发有趣而富有创意的营销小游戏

在数字化时代&#xff0c;企业通过创意而独特的方式与目标受众互动&#xff0c;已成为提高品牌知名度和用户参与度的重要手段之一。其中&#xff0c;设计一款引人入胜的营销小游戏&#xff0c;不仅能吸引用户的眼球&#xff0c;还能有效传达品牌信息。以下是一些建议&#xff0…

人工智能-注意力机制之注意力汇聚:Nadaraya-Watson 核回归

查询&#xff08;自主提示&#xff09;和键&#xff08;非自主提示&#xff09;之间的交互形成了注意力汇聚&#xff1b; 注意力汇聚有选择地聚合了值&#xff08;感官输入&#xff09;以生成最终的输出。 本节将介绍注意力汇聚的更多细节&#xff0c; 以便从宏观上了解注意力机…

canvas高级动画001:文字瀑布流

canvas实例应用100 专栏提供canvas的基础知识&#xff0c;高级动画&#xff0c;相关应用扩展等信息。 canvas作为html的一部分&#xff0c;是图像图标地图可视化的一个重要的基础&#xff0c;学好了canvas&#xff0c;在其他的一些应用上将会起到非常重要的帮助。 文章目录 示例…

基于官方YOLOv4开发构建目标检测模型超详细实战教程【以自建缺陷检测数据集为例】

本文是关于基于YOLOv4开发构建目标检测模型的超详细实战教程&#xff0c;超详细实战教程相关的博文在前文有相应的系列&#xff0c;感兴趣的话可以自行移步阅读即可&#xff1a;《基于yolov7开发实践实例分割模型超详细教程》 《YOLOv7基于自己的数据集从零构建模型完整训练、…

electron windows robotjs 安装教程

Robotjs 安装 前言第一步 : 安装python第二步 : 安装Visual Studio 2022第三步 : 安装robotjs 前言 robotjs可以控制鼠标键盘&#xff0c;获取屏幕内容&#xff0c;配合electron可做很多自动化操作。windows下配置环境有很多坑&#xff0c;很多文章都太旧了。试了很多次发现了…

杰发科技AC7801——Flash模拟EEP内存分布情况

简介 本文记录了在使用AutoChips芯片Flash模拟EEP过程中的一些理解 核心代码如下 #include <stdlib.h> #include "ac780x_sweeprom.h" #include "ac780x_debugout.h"#define SWEEPROM_SIZE (2048UL) /* Ssoftware eeprom size(Byte) */ #define TE…

在新疆乌鲁木齐的汽车托运

在新疆乌鲁木齐要托运的宝! 看过来了 找汽车托运公司了 连夜吐血给你们整理了攻略!! ⬇️以下&#xff1a; 1 网上搜索 可以在搜索引擎或专业的货运平台上搜索相关的汽车托运公司信息。在网站上可以了解到公司的服务范围、托运价格、运输时效等信息&#xff0c;也可以参考其他车…

播放器开发(四):多线程解复用与解码模块实现

学习课题&#xff1a;逐步构建开发播放器【QT5 FFmpeg6 SDL2】 前言 根据第一章内容&#xff0c;我们首先可以先把解复用和解码模块完成&#xff0c;其中需要使用到多线程以及队列&#xff0c;还需要使用FFmpeg进行解复用和解码动作的实现。 创建BaseQueue基类 BaseQueue.h…