Wolverine68 Posted June 18, 2009 Share Posted June 18, 2009 I need to create a customized HTTP object that will get the HTTP status code name and number then use Javascript code to display the HTTP status. Can't figure out how to do that. HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <head> <title>Extract Data from an XML file</title> <style type="text/css"> .showIt { font-size: 14pt; color: green; font-family: Arial, Tahoma, Verdana; border: thick solid; padding: 10px; } b { font-size: 16pt; color:#000000; } </style> <script type="text/javascript" language="javascript"> <!--The object detection code --> var req = false; // Is there support for native XHR object?: IE7+, Firefox, Safari, Opera if (window.XMLHttpRequest) { req = new XMLHttpRequest(); //create an XMLHttpRequest object } else if (window.ActiveXObject) //check for Version 6 { req = new ActiveXObject('MSXML2.XMLHTTP.6.0'); //create an ActiveX XMLHTTP component } if (!req) { req = new ActiveXObject('MSXML2.XMLHTTP'); //fallback to version 3 } function goXml() { if (req) { //Request data to be retrieved from the server req.onreadystatechange = function() { if (req.readyState == 4 && req.status == 200) { var response = req.responseXML; readXML(response); } } req.open("GET", "MusicianList.xml", true); req.send(null); } } function readXML(response) { var myResponse = response.documentElement; var myMusician = myResponse.getElementsByTagName("musician"); var place = document.getElementById("showIt"); for (var i=0; i < myMusician.length; i++) { place.innerHTML += "<b>Name: </b>" + myMusician[i].getElementsByTagName("name")[0].firstChild.nodeValue + "<br>"; place.innerHTML += "<b>Genre: </b>" +myMusician[i].getElementsByTagName("genre")[0].firstChild.nodeValue + "<br>"; place.innerHTML += "<b>Hitsong: </b>" +myMusician[i].getElementsByTagName("hitsong")[0].firstChild.nodeValue + "<br><br>"; } } //--> </script> </head> <form> Click the button to show the list: <input type="button" name="display" id="display" value="Display the List" onClick="goXml()"> </form> <p id="showIt" class="showIt"></p> </body> </html> XML file: <?xml version="1.0" encoding="utf-8" ?> - <musicians> - <musician> <name>Bruce Springsteen</name> <genre>Rock</genre> <hitsong>Born in the USA</hitsong> </musician> - <musician> <name>B.B. King</name> <genre>Blues</genre> <hitsong>The Thrill Is Gone</hitsong> </musician> - <musician> <name>Tim McGraw</name> <genre>Country</genre> <hitsong>Live Like You Were Dying</hitsong> </musician> - <musician> <name>Gordon Lightfoot</name> <genre>Folk</genre> <hitsong>Carefree Highway</hitsong> </musician> - <musician> <name>Glenn Miller</name> <genre>Big Band</genre> <hitsong>In The Mood</hitsong> </musician> </musicians> Quote Link to comment Share on other sites More sharing options...
jjacquay712 Posted June 18, 2009 Share Posted June 18, 2009 function getAjaxObject() { var ajaxObject; try { ajaxObject = new XMLHttpRequest(); } catch (e) { try { ajaxObject = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { ajaxObject = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return false; } } } return ajaxObject; } function sendRequest() { var request = getAjaxObject(); request.onreadystatechange = function() { if ( request.readyState == 4 ) { //Use this property request.status } } request.open("GET", "whatever_url.php", true); request.send(null); } Quote Link to comment Share on other sites More sharing options...
Wolverine68 Posted June 18, 2009 Author Share Posted June 18, 2009 Ok, but I'll need it to display codes from all five categories, 1xx, 2xx, 3xx, 4xx, 5xx. You wrote: request.onreadystatechange = function() { if ( request.readyState == 4 ) { //Use this property request.status Would I need to also include...?: If (request.readystate <> 4) && (request.readystate <> 200) { request.status Quote Link to comment Share on other sites More sharing options...
Wolverine68 Posted June 18, 2009 Author Share Posted June 18, 2009 Would I want to use try....catch with this block?: req.onreadystatechange = function() { if (req.readyState == 4 && req.status == 200) { var response = req.responseXML; readXML(response); } } req.open("GET", "MusicianList.xml", true); req.send(null); But, within catch, what statement would I use that would call the error message from one of the 5 categories of HTTP status codes? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.