<!-- const url = `https://api.vvhan.com/api/la.ji?lj=${encodeURIComponent("湿巾")}`; -->
<script>
// 定义一个url
const url = `https://api.vvhan.com/api/la.ji?lj=${encodeURIComponent(
"湿巾"
)}`;
// 下面要从这个url里面获取数据
// 开始写ajax
// new一个对象,下面都要用这个对象的属性
const xhr = new XMLHttpRequest();
// 为xhr设置readystate监听
xhr.onreadystatechange = () => {
// 看有没有完成接收数据
if (xhr.readyState !== 4) {
return false;
}
// 如果接收数据,就开始判断状态码
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
console.log(xhr.response);
}
// 定义open和send
xhr.open("get", url, true);
// 因为上面打印的是xhr的response,不是之前写的responseText
// 所以需要把打印出来的换成json的格式
xhr.responseType = "json";
xhr.send(null);
};
</script>