Use Ajax technology to log on to the home page through the XMLHttpRequest object, ajaxxmlhttprequest
Recently, Ajax technology is used to log on to a home page through the XMLHttpRequest object!
The Code is as follows:
<Script type = "text/javascript"> // create the XMLHttpRequest object function createXMLHttpRequest () {if (window. XMLHttpRequest) {return xmlhttprequest = new XMLHttpRequest ();} else {return xmlhttprequest = new ActiveXObject ("Microsoft. XMLHTTP ") ;}// method of running the logon Button function doStart () {var logname = document. getElementById ("loginName "). value; var logpass = document. getElementById ("loginPsw "). value; var userinfo = "inAccount =" + logname + "& inPsw = "+ Logpass; var url =" users/users_pswCheck.action "; xmlhttprequest = createXMLHttpRequest (); xmlhttprequest. onreadystatechange = getresultValue; xmlhttprequest. open ("post", url, true); xmlhttprequest. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); xmlhttprequest. send (userinfo);} // callback method function getresultValue () {if (xmlhttprequest. readyState = 4 & xmlhttprequest. status = 200) {var result = xmlhtt Prequest. responseText; if (result = "success") {window. location. href = "index. jsp "rel =" external nofollow ";} else {document. getElementById ("xiaoxi "). innerHTML = "Logon Failed! ";}}// Button event on the page, that is, the function keybutton () {if (event. keyCode = 13) {doStart (); return ;}</script>
Question about AJAX initialization of XMLHttpRequest object
Function GetXmlHttpObject () {var xmlHttp = null; try {// Firefox, Opera 8.0 +, SafarixmlHttp = new XMLHttpRequest ();} catch (e) {// Internet assumertry {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}} return xmlHttp;} This is the creation of the xmlhttprequest object in the standard w3c
How to create an ajax XMLHttpRequest object
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP"); or xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP "); the two lines of code basically try to use a version of MSXML to create the XMLHttpRequest object. If it fails, use another version to create the XMLHttpRequest object. If the Code must support both Internet Explorer and non-Microsoft browsers. The following code is displayed. <Script language = "javascript" type = "text/javascript" var xmlHttp = false; try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {try {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ") ;}catch (ee) {xmlHttp = false ;}} if (! XmlHttp & typeof XMLHttpRequest! = 'Undefined') {xmlHttp = new XMLHttpRequest () ;}</script the core of this Code is divided into three steps: 1. Create a variable xmlHttp to reference the XMLHttpRequest object to be created. 2. Try to create this object in Microsoft browser: Try to create it using the Msxml2.XMLHTTP object. If it fails, try the Microsoft. XMLHTTP object again.