1 <!DOCTYPE HTML>2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />5 <title></title>6 <Scripttype= "Text/javascript">7 /**8 * Create XMLHttpRequest Objects9 */Ten functioncreatexhr () { One varXHR= NULL; A if(window. XMLHttpRequest) { - XHR= NewXMLHttpRequest (); - } the Else { - XHR= NewActiveXObject ("Microsoft.XMLHTTP"); - } - returnxhr; + } - + functiontestxhr () { A varXHR=createxhr (); at Window.alert (XHR); - } - - functiongetservertest () { - //Get XHR Object - varXHR=createxhr (); in //Create request - Xhr.open ("Get", "ajax.ashx", true); to //Setting the callback function + Xhr.onreadystatechange= function () { - if(Xhr.readystate== 4 &&Xhr.status== $) { the //the server has processed the request and correctly responded to the data to the client * varResText=Xhr.responsetext; $ document.getElementById ("hh"). InnerText=ResText;Panax Notoginseng } - } the //4. Sending the request + Xhr.send (NULL); A } the </Script> + </Head> - <Body> $ <inputtype= "button"value= "Test Xhr"onclick= "testxhr ()"> $ <H2ID= "hh"></H2> - <ahref= "Javascript:getservertest ()">Submit data</a> - </Body> the </HTML>
Implementing Ajax with Pure XMLHttpRequest