题目
知识点
手写原生Ajax请求
// get 请求
// 初始化ajax实例
const xhr = new XMLHttpRequest()
// true 表示异步请求, false 表示同步请求
xhr.open(‘GET‘, ‘/api‘, true)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(xhr.responseText)
} else {
console.log(‘其他情况‘)
}
}
}
xhr.send(null)
// post 请求
// 初始化ajax实例
const xhr = new XMLHttpRequest()
// true 表示异步请求
xhr.open(‘POST‘, ‘/api‘, true)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
alert(xhr.responseText)
} else {
console.log(‘其他情况‘)
}
}
}
const postData = {
userName: ‘zhangsan‘,
password: ‘123456‘
}
xhr.send(JSON.stringify(postData))
xhr.readyState
取值含义
send()
方法send()
方法,正在发送请求send()
方法执行完成,已经接收到全部响应内容只有在 readyState
的值为 4 的时候才能拿到 responseText
并使用
xhr.status
取值
什么是同源策略(浏览器中)?
加载图片 css js 可以无视同源策略
<img src=跨域的图片地址>
如果引用的图片网站做了图片的防盗链,那么浏览器就无法加载这个图片<link href=跨域的css地址>
<script src=跨域的js地址><script>
<img />
可用于统计打点,可使用第三方统计服务<link /> 和 <script>
可使用 CDN,CDN一般都是外域<script>
可实现 JSONP跨域
<script>
可绕过跨域限制<script>
可以获得跨域的数据,只要服务端允许dmeo
<script>
window.jsonpFun = function (data) {
console.log(data)
}
</script>
<script src=‘http://localhost:8081/jsonp.js?name=zhangsan&callback=jsonpFun‘></script>
jQuery 实现 ajax跨域
$.ajax({
url: ‘http://localhost:8081/jsonp.js‘,
dataType: ‘jsonp‘,
jsonpCallback: ‘callback‘,
success: function (data) {
console.log(data)
}
})
CORS-服务器设置 http header
CORS跨域是通过后端实现的,如果后端设置了CORS跨域,那么浏览器就可以直接请求
手写简易ajax请求
function ajax(url) {
const p = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open(‘GET‘, url, true)
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
resolve(JSON.parse(xhr.responseText))
} else {
reject(new Error(‘error‘))
}
}
xhr.send(null)
})
return p
}
const url = ‘/data/test.json‘
ajax(url)
.then(res => {console.log(res)})
.catch(err => {console.log(err)})
实际中常用的ajax插件:jquery使用ajax;fetch(兼容性不好);axios(多用于框架中)
原文:https://www.cnblogs.com/aurora-ql/p/13388049.html