ajax 格式
- 原生 js 写法
let ajax = new XMLHttpRequest()
// open 方法 以 get 方式 打开 路径为 ./a.txt 异步方式(异步为 true,同步为 false)
ajax.open('GET','./a.txt',true)
// 发送请求
ajax.send()
// 监听事件
ajax.onreadystatechange = () =>{
// 完成并且成功
if(ajax.readyState === 4){
console.log('请求完成')
if(ajax.status === 200){
// 请求成功 输出数据
console.log('请求成功')
console.log(ajax.response) // Hello
}
}
}
- jquery 写法 ,$.ajax() 方法
$.ajax({
url:'./a.txt',
async:true,
type:'GET',
success(data){
console.log(data) // Hello
}
})
- jquery 写法 ,$.get() 方法
$.get({
url:'./a.txt',
success(data){
console.log(data) // Hello
}
})