Ajax:在不刷新页面的情况下,异步的服务器进行交互;
示例:
function getAjax(httpUrl,fn){
//1实例化xhr对象
var xhr = new XMLHttpRequest()
//2设置请求的路径和方法
xhr.open("GET",httpUrl);
//3发送请求
xhr.send()
//4监听事件,接收请求
xhr.onreadystatechange = function(){
if(xhr.readyState==4&&xhr.status==200){
/*console.log(xhr.readyState);
console.log(xhr.status)
console.log(xhr)*/
fn(xhr)
}
}
}
getAjax("http://127.0.0.1:8020/5-1ajax/hello.txt",function(xhr){
var h1 = document.createElement("h1");
h1.innerHTML = xhr.responseText;
h1.style.color = "pink"
document.body.appendChild(h1)
})