To create an asynchronous object:
function Createxmlhttp () {var xhobj=false;try{xhobj=new activexobject ("Msxml2.xmlhttp");} catch (e) {try{xhobj=new ActiveXObject ("Microsoft.XMLHTTP");} catch (E2) {Xhobj=false;}} if (!xhobj&&xmlhttprequest!= ' undefined ') {xhobj=new xmlhttprequest ();} return xhobj;}
Get Request:
Window.onload=function () { document.getElementById ("Btnclick"). Onclick=function () { //The first step creates an asynchronous object. var xhr=createxmlhttp (); Xhr.open ("Get", "Modify.ashx", true); Xhr.setrequestheader ("If-modified-since", 0); Xhr.onreadystatechange=function () { if (xhr.readystate==4&&xhr.status==200) { var res= Xhr.responsetext; document.getElementById ("Divmsg"). Innerhtml=res; } } Send request xhr.send (null) };};
Post Request:
Window.onload=function () { document.getElementById ("Btnreg"). Onclick=function () { var xhr=createxmlhttp () ; Define a turn interface var urlpara= "05kjdfgsdjf.ashx" xhr.open ("Post", urlpara,true); Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); 5.0 set the callback function. xhr.onreadystatechange=function () { if (xhr.readystate==4&&xhr.status==200) { var res= Xhr.responsetext; Process data ... } 6.0 Send request Name=james & pwd=123 xhr.send ("Name=" +document.getelementbyid ("Txtname"). Value+ "&pwd=" + document.getElementById ("Txtpwd"). value);} }
JavaScript Ajax Implementation Learning