Ajax:在不刷新頁(yè)面的情況下,異步的服務(wù)器進(jìn)行交互;
示例:
function getAjax(httpUrl,fn){
????????//1實(shí)例化xhr對(duì)象
????????var xhr = new XMLHttpRequest()
????????//2設(shè)置請(qǐng)求的路徑和方法
????????xhr.open("GET",httpUrl);
????????//3發(fā)送請(qǐng)求
????????xhr.send()
????????//4監(jiān)聽(tīng)事件,接收請(qǐng)求
????????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)
})