This article mainly introduces JS Basic Ajax writing, the need for friends can refer to the following
The code is as follows: var xmlhttp = null; function Myajax () { //1, create XMLHttpRequest object //2, You need to write different code if (Windows) in different ways for IE and other browsers to create this object. XMLHttpRequest) { //for ff,mozilar,opera,safari,ie7,ie8 xmlhttp = new XMLHttpRequest (); //fix some browsers bug& nbsp if (xmlhttp.overridemimetype) { xmlhttp.overridemimetype ("Text/xml"); } } else if (window. ActiveXObject) { //IE6 below for browsers var activexname = ["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "," ""]; for (var i = 0; i < activexname.length; i++) { Try { //Remove a control name creation, if the creation succeeds then stops, conversely throws the exception XMLHTTP = new ActiveXObject (activexname[i)); break;& nbsp catch (e) {} } } //need to confirm XMLHTTP creation was successful if (!xmlhttp) { alert ("XMLHttpRequest Create failed!") ; return; } else { alert (XMLHTTP); } //Registration callback function. Note the registration callback function is not parenthesized and adds the value of the function back to onreadystatechange Xmlhttp.onreadystatechange = callback; //Set Connection information // The first parameter represents the HTTP request method, which supports all HTTP request methods, mainly makingThe URL address of the request is represented by a get and post //second parameter, and the parameter requested by the Get method also represents an asynchronous or synchronous interaction in the urlkh //third interface parameter, true for the asynchronous Xmlhttp.open ("Get" , "servlet/checkusername?username=" + UserName, True); //Send data representations and server-side interaction //sync, send this will be done after server-side data comes back Xmlhttp.send (NULL); //asynchronous mode, send this sentence completes the execution of //post mode request code //xmlhttp.open immediately ("POST", " Servlet/checkusername ", True"; //post method requires you to set HTTP request headers //xmlhttp.setrequestheader ("Content-type", " Application/x-www-form-urlencoded "); //post mode send data //xmlhttp.send (" username= "+username); } //Callback functions function callback () { //Judge the state of the object is interactive completion if (xmlhttp.readystate = 4) { //To determine whether HTTP interaction is successful if (xmlhttp.status =) { //Get server-side returned data //Get server-side output of plain text data var responsetext = Xmlhttp.responset ext; alert (responsetext); } } }