Use Ajax technology to log on to the home page through the XMLHttpRequest object, ajaxxmlhttprequest

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.