user2430338
user2430338

Reputation: 89

Server not available

How and where do I add the message "Server not available" if I have a code that calls a php file from a remote server but the php file is unavailable? Here's my code:

<html>
<head>
    <script language="Javascript"> 
        function View(){
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("datatable").innerHTML=xmlhttp.responseText;    
            }
            xmlhttp.open("POST", "http://someremoteserver/somephpfile.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttp.send(); 
        }
    </script>
</head>

<body onload="View();" >
    <div id="datatable" align="center"></div>
</body>
</html>

Upvotes: 0

Views: 230

Answers (1)

colestrode
colestrode

Reputation: 10668

You could detect the different HTTP Status codes and do something based on the response. Or you could just implement a generic handler if the status isn't 200:

if (xmlhttp.readyState==4) {
    if (xmlhttp.status==200) {
        document.getElementById("datatable").innerHTML=xmlhttp.responseText;    
    } else if (xmlhttp.status == 503) { //specific error code
        alert('Sorry, this server is unavailable');
    } else { //generic error handler
        alert('There was an error with your request');
    }
}

Upvotes: 3

Related Questions