javascript - What does XMLHttpRequest do in that code -
i'm beginner in javascript , , want understand method xmlhttprequest does.
this code reading, , wondering if explain doing:
var xhttp; xhttp=window.xmlhttprequest?new xmlhttprequest:new activexobject("microsoft.xmlhttp"),xhttp.open("get","script.php",!0),xhttp.send();
hi not in explanations, try explain in details see , understand this.
the xmlhttprequest object. used exchanging of data server. use can send data script on server(request) , data it(response). response data can displayed instantly on page without page reloading. process call ajax.
i read provided code as
//define variable var xhttp; /*assign xmlhttprequest object variable check if global object window has xmlhttprequest object if not , user have newer browser, create 1 (new xmlhttprequest - ie7+, firefox, chrome, opera, safari browsers) or user have older browser (activexobject("microsoft.xmlhttp") - ie6, ie5 browsers) xhttp.open method specifies type of request(method get, script on server, asynchronous) xhttp.send method sends request server*/ xhttp=window.xmlhttprequest?new xmlhttprequest:new activexobject("microsoft.xmlhttp"),xhttp.open("get","script.php",!0),xhttp.send(); but have check readystate property of xmlhttprequest object well
xmlhttp.onreadystatechange = function() { //4: request finished , response ready //200: "ok" if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { //display of returned data server //it available in property - xmlhttp.responsetext } } the whole peace of code should like:
if (window.xmlhttprequest) { xmlhttp = new xmlhttprequest(); // code ie7+, firefox, chrome, opera, safari } else { xmlhttp = new activexobject("microsoft.xmlhttp"); // code ie6, ie5 } xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { //display of returned data server //jquery example $('div').html(xmlhttp.responsetext); } } xmlhttp.open("get", "script.php", true); xmlhttp.send(); hopefully helped, luck!
Comments
Post a Comment