AJAX即“Asynchronous Javascript And XML”(异步的JavaScript和XML),是指一种创建交互式网页应用的网页开发技术,用于浏览器和服务器之间进行数据交互。
1. get无参数
// 1.创建实例对象
var xhr = new XMLHttpRequest();
// 2.打开一个连接
xhr.open("get", 'http://47.101.202.33:8002/index/findAllCategory');
// 3.发送请求
xhr.send();
// 4.接收响应
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 & xhr.status === 200) {
console.log(xhr.responseText)
}
}
2. get携带参数
var qs = Qs;
// 1.创建一个实例
var xhr = new XMLHttpRequest();
// 2.打开一个连接
let obj = {
page: 1,
pageSize: 10
}
console.log(qs.stringify(obj))
xhr.open('get', "http://47.101.202.33:8002/index/pageQueryArticles" + '?' + qs.stringify(obj));
// 3.发送请求
xhr.send();
// 4.接收响应
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 & xhr.status === 200) {
console.log(xhr.responseText)
}
}
3. post携带参数
// 1.创建一个实例
var xhr = new XMLHttpRequest();
// 2.打开一个连接
let obj = {
username: 'admin1',
password: 123321
}
xhr.open('post', "http://47.101.202.33:8002/user/login");
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(
JSON.stringify(obj)
);
// 4.接收响应
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 & xhr.status === 200) {
console.log(xhr.responseText)
}
}