Jump to content

HTTP status code


Wolverine68

Recommended Posts

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>

 

     

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.