We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,590 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Handling 404 server response in Ajax?

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.

2
Contributors
4
Replies
2 Hours
Discussion Span
3 Months Ago
Last Updated
7
Views
Question
Answered
yaragalla_mural
Light Poster
31 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

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> 
yaragalla_mural
Light Poster
31 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
if (http.readyState == 4 && http.status == 200)

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

pritaeas
Posting Prodigy
Moderator
9,265 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,456
Skill Endorsements: 86

Thank you so much.

yaragalla_mural
Light Poster
31 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 3 Months Ago by pritaeas

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0685 seconds using 2.68MB