Created
July 24, 2017 08:45
-
-
Save hsipeng/2006aa8c9c6e61d553bf6c91c1ca8134 to your computer and use it in GitHub Desktop.
传统ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| document.getElementById("b1").onclick=function () { | |
| //AJAX调用 | |
| /** | |
| 创建xmlHttpRequest | |
| 绑定回调函数 | |
| 与服务器建立连接 | |
| 发送数据 | |
| 回调函数处理相应的数据 | |
| */ | |
| var xmlhttp = createXMLHttpRequest(); | |
| xmlhttp.onreadystatechange = function () { | |
| if(xmlhttp.readyState == 4){ | |
| if(xmlhttp.status == 200){ | |
| var callback = xmlhttp.responseText; | |
| alert(callback) | |
| } | |
| } | |
| } | |
| xmlhttp.open("POST","../servlet/AjaxServlet",true); | |
| xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); | |
| xmlhttp.send("name=张三"); | |
| } | |
| function createXMLHttpRequest() { | |
| try{ | |
| // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码 | |
| xmlhttp = new XMLHttpRequest(); | |
| }catch(tryMS){ | |
| try{ | |
| //IE6, IE5 浏览器执行的代码 | |
| xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); | |
| }catch(otherMS){ | |
| try{ | |
| xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); | |
| }catch(failed){ | |
| xmlhttp = null; | |
| //无法获得xmlhttp对象 | |
| } | |
| } | |
| } | |
| return xmlhttp; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment