2

There is a lot of same questions but i cannot find the answer so i am reposing the same question.I am receiving an json request using java script

    var request = null;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        return null;
    }
    request.open("GET", url, true);
    request.send(null);

    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200) {
}
} else
                alert(request.status);
        }

I am receiving an status code zero while i receive the request the javascript runs on apache server and json request will be received from tomcat server everything is local . can any one tell me how to over come it. Note: i cannot use any framework.

Ramesh
  • 2,295
  • 5
  • 35
  • 64
  • 2
    Just for your info: There is a small mistake in your code. Replace 'rquest' by 'request'. – Reporter Jan 13 '12 at 08:59
  • May be http://stackoverflow.com/questions/8832359/jquery-ajaxrequest-xhr-status-code-0-but-html-status-code-200 can help – Reporter Jan 13 '12 at 09:00

1 Answers1

2

I suspect that you are violating the same origin policy restriction that is built in browsers. This restriction prevents you from sending cross domain AJAX requests. So for example if the page containing the AJAX call is hosted on http://localhost/example.htm and you are trying to send an AJAX request to http://localhost:8080/somescript you won't be able to do so because the domains do not match (different ports).

The best way to ensure that your AJAX requests work is by only using relative urls:

request.open("GET", "/somescript", true);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928