<script>
// 定义一个url
const url = `https://api.vvhan.com/api/la.ji?lj=${encodeURIComponent("手机")}`;
// 开始写ajax
// 用new先定义一个对象xhr
const xhr = new XMLHttpRequest();
// 为xhr添加事件监听
// readystate
xhr.onreadystatechange = () => {
// 是否请求了全部数据
if (xhr.readyState !== 4) {
return false;
}
// 如果readyState是4,继续往下
// 判断状态码
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
console.log("数据请求成功");
console.log(xhr.responseText);
}
};
// 定义open
xhr.open("get", url, true);
// 定义send
xhr.send(null);
</script>