0

Added GET response header. Please see if u get any clue... Thanks for your time

*Revised question with new code and the results * I have a set of json files web hosted by mongoose in my localhost. I want to read one of those json files,parse it and display in a HTML page as a table. This HTML based table is to be later consumed by another application.

Please note that the mongoose web server is just hosting the files. I am not sure if it will support any GET/POST requests.

Trying to read the file thru Ajax request. Here is my code with the console output below. Please advise.

<html>
    <head>
    <meta charset="UTF-8">
    <title>ajaxtester</title>
    <script type="text/javascript">
        function loadXMLDoc(){  var xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        } 
        else {
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xhr.onreadystatechange = function() 
        {   console.log("within function");
            if (xhr.readyState==4 && xhr.status==200) {

                console.log("If check passed")
                console.log(xhr.readyState);
                console.log(xhr.status);
                document.getElementById("myDiv").innerHTML=xhr.responseText;
            }
            else
            {   
                console.log("Failed If check")
                console.log(xhr.readyState);
                console.log(xhr.status);
            }
        }
        console.log("After rdystate function")
        console.log(xhr.readyState);

        xhr.open("GET","http://localhost:8080/iso.txt",true);       
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send();

        console.log("SEND");
        console.log(xhr.readyState);
        console.log(xhr.status);
        console.log(xhr.statusText);
    }
    </script>
    </head>
    <body>
        <div id="myDiv">
        <p>Let AJAX change this text!</p>
        </div>
        <button type="button" onclick="loadXMLDoc()">Change Content</button>
    </body>

After rdystate function 0

within function Failed If check 1 0

GET http://localhost:8080/iso.txt 200 OK 2ms

SEND 1 0 (an empty string)

within function Failed If check 2 0

within function Failed If check 4 0

As you see there is a GET request with status code 200, but xhr.status always returns 0. Also the GET response header in firebug shows the content length correctly. But the actual response remains elusive.

Response header of GET request. Note the connection-close, Is this a normal behavior ? Response Headers Accept-Ranges bytes Connection close Content-Length 5 Content-Type text/plain Date Fri, 03 Jan 2014 15:29:41 GMT Etag "52c59fe6.5" Last-Modified Thu, 02 Jan 2014 17:20:38 GMT

bukli
  • 172
  • 2
  • 9
  • 1
    Are there any errors appearing in your console? – Daniel W. Jan 02 '14 at 08:34
  • 1
    Have you tried adding breakpoints? console.log statements? Is your code actually being executed? – ColinE Jan 02 '14 at 08:35
  • What does your bus_perf.json file look like? Are you getting a response? Are there any errors in your console? Besides, you should try smaller, if you don't have any experience with JavaScript then you should probably start with simpler problems first. – Jonast92 Jan 02 '14 at 08:35
  • First thanks to Dan,Colin and Jonast. I don't fully understand most of your questions. Anyways will start with simpler problems as Jonast said. Let me try to consume the file using FILE API. – bukli Jan 02 '14 at 09:41
  • Hi rob, If your finding the above questions difficult you may find this post useful [How do I debug my JavaScript](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Jan 02 '14 at 14:47
  • I would also recommend getting yourself a copy of [fiddler](http://fiddler2.com/). This will allow you to see the RAW HTTP requests which may well help you out. – Liam Jan 02 '14 at 14:48

1 Answers1

0

Found the answer here http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/282972/why-am-i-getting-xmlhttprequest.status0

I changed the url of my script browser to localhost:8080/readfile.html and it worked like a magic. No other changes made.

bukli
  • 172
  • 2
  • 9