javascript - Using AJAX, and retrieving the ready states -
i using code behind retrieving ready states , it's works me. may please ask how can add else statemen in content. if url not available print message user. like
document.getelementbyid("div p").innerhtml = "ping not success!";
and try make looks little bit more fancier plain html text. code:
url = "<whatever want ping>" ping = new xmlhttprequest(); ping.onreadystatechange = function(){ document.body.innerhtml += "</br>" + ping.readystate; if(ping.readystate == 4){ if(ping.status == 200){ result = ping.getallresponseheaders(); document.body.innerhtml += "</br>" + result + "</br>"; } } } ping.open("get", url, true); ping.send();
when requests ends, readystate
4
, status
may value other 200
. if status
0
, indicates network error or cors failure (for cross-origin servers). if other value (like 404
), means script reached server, server didn't handle request (either because url path used wasn't meaningful, or server-side error occurred, etc.)
ping.onreadystatechange = function(){ document.body.innerhtml += "<br>" + ping.readystate; if(ping.readystate == 4){ if(ping.status == 200){ result = ping.getallresponseheaders(); document.body.innerhtml += "<br>" + result + "<br>"; } else if(ping.status == 0) { document.getelementbyid("foobar").innerhtml = "ping not successful! not response server, due network failure, cors error, or offline server."; } else { result = ping.getallresponseheaders(); document.getelementbyid("foobar").innerhtml = "we got response server, status code " + ping.status; } } }
Comments
Post a Comment