1.11M Members

Handling 404 server response in Ajax?

 
0
 

Hi i am working with an web application. in my webapp on the browser i am intiating an ajax call to update part of the page. I get the response as html from the server and i will insert the complete response into the div which displays it. I got a serious problem here. when server throws a page with 404 error then i should not display anything. I mean i should ignore that response. How this can be done? Thanks in advance. I am struggling with this from so long.

 
0
 

What's the code you are using. An AJAX call returns a status you can use.

 
0
 

The following is my code. please have a look at it.

function getHTTPObject() {
        if (typeof XMLHttpRequest != 'undefined') {
            return new XMLHttpRequest();
        }
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
        return false;
    }




    <script type="text/javascript">                      
            function getSearchStudents(){
                document.getElementById("noStudent").style.visibility = "hidden";
                var userString=document.getElementById("userString").value;
                var http=getHTTPObject();
                http.open("GET", "searchStudent.do?userString="+userString, true);              
                http.onreadystatechange = function() {
                    if (http.readyState == 4) {
                        document.getElementById("container").innerHTML=http.responseText;
                        if(http.responseText=="")
                            document.getElementById("noStudent").style.visibility = "visible";
                            if(http.responseText.length==2)
                                document.getElementById("noStudent").style.visibility = "visible";
                    }
                }
                http.send("");              
            }           
        </script> 
 
0
 
if (http.readyState == 4 && http.status == 200)

status respresents the HTTP response code, 200 is OK, it can also be 404.

 
0
 

Thank you so much.

Question Answered as of 1 Year Ago by pritaeas
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: