AJAX
- AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML)
- AJAX最主要的两个特性:在不重新加载页面的情况下发送请求给服务器;受并使用从服务器发来的数据
AJAX请求的五个步骤
- 1.建立XMLHttpRequest对象
- 2.设置回调函数
- 3.配置请求信息,(如open,get方法),使用open方法与服务器建立链接
- 4.向服务器发送数据
- 5.在回调函数中针对不同的响应状态进行处理
readyState
- 0 -(请求未初始化)还没有调用send()方法
- 1 -(正在加载)已调用send()方法,正在发送请求
- 2 -(加载成功)send()方法执行完成,已经接收到全部响应内容
- 3 -(交互)正在解析响应内容
- 4 -(完成)响应内容解析完成,可以在客户端调用了
使用
function ajax(url, method="GET", data=null, async=true) {
let XHR = XMLHttpRequest;
XHR = new XMLHttpRequest()
XHR.open(method, url, async)
XHR.responseType = 'json'
XHR.setRequestHeader("Content-type", "text/html");
XHR.onreadystatechange = function() {
if (XHR.readyState === 4 ) return
console.log(`响应状态:${XHR.status}`,"FINISH")
if(XHR.status == 200) {
var res = XHR.responseText
console.log(res)
}
}
XHR.onerror = function() {
console.log('Network request failed')
}
XHR.send(data)
}
ajax("https://www.baidu.com");
ajax("https://www.baidu.com","POST","A=1&B=2");
参考
Fetch
- Fetch是浏览器内置API,在浏览器环境中,可以通过顶层对象window获取
- fetch()是XMLHttpRequest(ajax使用的就是XMLHttpRequest)的升级版,用于在JavaScript脚本里面发出HTTP请求
语法
let promise = fetch(url, [options])
GET请求
fetch(URL)
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log('Request Failed', err));
使用await语法
async function fetchText() {
let response = await fetch(URL);
console.log(response.status);
console.log(response.statusText);
}
POST请求
const response = await fetch(URL, {
method: 'POST',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
},
body: 'foo=bar&lorem=ipsum',
});
const json = await response.json();
POST提交JSON数据
const user = { name: 'John', surname: 'Smith' };
const response = await fetch(URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(user)
});
POST提交表单数据
const form = document.querySelector('form');
const response = await fetch(URL, {
method: 'POST',
body: new FormData(form)
})
文件上传
const input = document.querySelector('input[type="file"]');
const data = new FormData();
data.append('file', input.files[0]);
data.append('user', 'foo');
fetch(URL, {
method: 'POST',
body: data
});
上传二进制数据
let blob = await new Promise(resolve =>
canvasElem.toBlob(resolve, 'image/png')
);
let response = await fetch(URL, {
method: 'POST',
body: blob
});
取消fetch()请求
- fetch()请求发送以后,如果中途想要取消,需要使用AbortController对象
let controller = new AbortController();
let signal = controller.signal;
fetch(url, {
signal: controller.signal
});
signal.addEventListener('abort',
() => console.log('abort!')
);
controller.abort();
console.log(signal.aborted);
Axios
GET请求
axios.get('/user?ID=5422')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios.get('/user', {
params: {
ID: 5422
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios({
method: 'get',
url: 'http://www.xxx.com/test',
responseType: 'stream'
})
.then(function(response) {
response.data.pipe(fs.createWriteStream('xxx.jpg'))
});
POST请求
axios.post('/user', {
firstName: '三',
lastName: '张'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
axios({
method: 'post',
url: '/user/001',
data: {
firstName: '三',
lastName: '张'
}
});
并行请求
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
}));
创建实例
let instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'custom header content'}
});
Response
axios.get('/user/12345')
.then(function(response) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});
Config
axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
var instance = axios.create({
baseURL: 'https://api.example.com'
});
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
var instance = axios.create();
instance.defaults.timeout = 2500;
instance.get('/longRequest', {
timeout: 5000
});
拦截器
axios.interceptors.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
return Promise.reject(error);
});
var myInterceptor = axios.interceptors.request.use(function () {});
axios.interceptors.request.eject(myInterceptor);
var instance = axios.create();
instance.interceptors.request.use(function () {});
错误处理
axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
console.log(error.request);
} else {
console.log('Error', error.message);
}
console.log(error.config);
});
axios.get('/user/12345', {
validateStatus: function (status) {
return status < 500;
}
})
取消请求
var CancelToken = axios.CancelToken;
var source = CancelToken.source();
axios.get('/user/12345', {
cancelToken: source.token
}).catch(function(thrown) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else {
}
});
axios.post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
})
source.cancel('Operation canceled by the user.');
var CancelToken = axios.CancelToken;
var cancel;
axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) {
cancel = c;
})
});
cancel();