ajaxjspservletssafarionreadystatechange

ajax ready state reachs 4 but status always is 200


my ajax code is:

 xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        alert(xhr.status);
        if (xhr.status == 200) {
            alert("200");               
        }
        else if (xhr.status == 8001) {
            alert("8001");               
        }
        else if (xhr.status == 8000) {
            alert("8000");             
        }
        else if (xhr.status == 7000) {
            alert("7000");                
        }
        else {
            alert("x");              
        }
    }
};
xhr.open("POST","URL",true);
xhr.send(null);

and my code in page that i addressed in xhr.open is:

    if (request.getSession().getAttribute("SATI_UID") == null) {
        response.sendError(8000);
    } else {
            response.sendError(8001);
    }

it works in firefox and chrome and ie but in safari i always get status 200. i use safari 5.1.7. i also use response.setStatus instead of response.sendError but it's not working. anybody knows why this happens?


Solution

  • You need to set the status of the response server side, on your servlet.

    The AJAX client will get the response and store the status code on xhr.status.

    It is supposed to be a valid HTTP status code.

    Don't forget the response code has to be set before the response is commited and any headers are sent.

    You might want to use HttpServletResponse#setStatus or HttpServletResponse#sendError to do so.

    Or you may want to define an error page on your web.xml instead. Check out: How to set HTTP status code in JSP error handlers