0

i am having a problem with the below code , it whenever i would try to request a the JSON data from the website , i would always get back a response code 0. Does anyone know why? If i were to go the website i would get the data just by inputting the correct login information.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="Base64.js" type="text/javascript"></script>
    <script type="text/javascript">

        function alertContents(HTTPobj) {

            if (HTTPobj.readyState == 4) {
                // everything is good, the response is received
                if ((HTTPobj.status == 200) || (HTTPobj.status == 0)) {
                    // FIXME: perhaps a better example is to *replace* some text in the page.
                    var htmlDoc = document.createElement('div'); // Create a new, empty DIV node.
                    htmlDoc.innerHTML = HTTPobj.responseText; // Place the returned HTML page inside the new node.
                    alert("The response was: " + HTTPobj.status + HTTPobj.responseText);
                    //var jsonData = eval('(' + HTTP.responseText + ')');
                    //parseJson(HTTP.responseText);

                }
                else {
                    alert('There was a problem with the request. ' + HTTPobj.status + HTTPobj.responseText);
                }
            }
        }


        }

        function makeBaseAuth(user,password) {
            var tok = user + ':' + password;
            var hash = Base64.encode(tok);
            alert(hash);
            return "Basic " + hash;
        }

        function getInput() {

            var getUser = document.input.user.value;
            var getPass = document.input.pass.value;
            var logData = makeBaseAuth(getUser, getPass);
            var url = 'http://www.tt-galaxi.com/thubrest/login';
            //  var url = 'http://www.tt-galaxi.com/thubrest/users/%@/trips',username;
            //    var url = 'http://www.tt-galaxi.com/thubrest/parkingbays';
            var HTTPobj = new XMLHttpRequest();
            HTTPobj.onreadystatechange = function () { alertContents(HTTPobj); };
            var test = HTTPobj.open('GET', url);
            HTTPobj.setRequestHeader('Authorization', logData);
            HTTPobj.send();

        }



    </script>
    </head>
    <body>

    <form name="input" action="">
    Username: <input type="text" name="user" />
    <br />
    Password:<input type="text" name="pass" />    
    <br />
    <input id="Button1" type="button" value="Submit"  onclick="getInput()"/>
    </form>
    </body>
    </html>
sutoL
  • 1,737
  • 10
  • 27
  • 48
  • What browser are you using? And is there any error on console? – Subir Kumar Sao Jun 06 '12 at 07:28
  • I'm not into pure javascript, but if you use jQuery's post function it will make your life much easier – evilReiko Jun 06 '12 at 07:30
  • i have tried IE,FF,Chrome,Opera , they are all giving the same response code. I am thinking it is related to the same origin policy, however i do not have an idea on how to work on it. – sutoL Jun 06 '12 at 07:36
  • You won't be able to do this cross domain, so yes, that is probably the problem. You'll either need to proxy it locally (with a server script that performs the query as a local resource) or use a JSONP interface (if the endpoint supports it). – Hamish Jun 06 '12 at 07:42
  • i am running the above code from my local machine hosted on IIS 7.0 , does that constitutes to cross domain? – sutoL Jun 06 '12 at 07:48

2 Answers2

0

Looks like you are trying to request data from a host that is of different origin than yours. To elaborate this, if you have a site called www.example.com running in host A and you try to make a request to wwww.somethingelse.com running in host B, you would not be able to get the data from host B because most modern browsers enforce the same origin policy that prohibits the use of AJAX request in cross-site manner. If the browser detects this kind of "illegal request", it blocks the incoming data from that different host.

However, if you own that other host, you can explicitly pass a header to the browser which basically tells the browser to allow cross-site data from that other host. Read more about this here.

hyde
  • 2,525
  • 4
  • 27
  • 49
  • hi, thanks, is there anyway to disable this because i can be sure this other host would be reliable. Also i do not own the other host. And this website is to be accessed via mobile phones. – sutoL Jun 06 '12 at 07:54
0

The status code will be 0 for illegal cross-origin requests, see XMLHttpRequest status 0 (responseText is empty) and XMLHTTPRequest.status returns 0 and responseText is blank in FireFox 3.5.

Also, on FF the status code will be 0 for local file:// requests.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375