但是,如果前台服务员将小明的菜单告诉厨师后,服务员发给小明一个好牌去旁边等待,后面的人继续点餐,厨师将小明的饭菜做好后,随时呼唤小明就餐;这就是异步处理
服务器的不同做法,就代表着 Ajax 的同步或异步处理;小明就是客户端;厨师就是后台服务器;
ajax前端代码:
!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> 姓名:<input type="text" class="ipt"> <span id="spa"></span> </body> <!-- <script src="./ajax.js"></script> --> <script> // console.log(window) var ipt = document.getElementsByClassName('ipt') ipt[0].onblur = function(){ var xhr = new XMLHttpRequest() xhr.onreadystatechange = function(){ if(xhr.readyState==4){ if(xhr.responseText==1){ var s = '<font color="red">不可用</font>' document.getElementById('spa').innerHTML=s; }else{ var s = '<font color="red">可用</font>' document.getElementById('spa').innerHTML=s; } } } xhr.open('post','http://127.0.0.1:8090/gets',true)//如果什么都不写默认是异步的。//true 异步 //false同步//
xhr.send('me='+ipt[0].value)
}
</script> </html>