1

I created a json file with some objects, and I build HTML page that if I press a button, all the objects in the json file will apear on the page. I run the JSON file with ECLIPSE on the server, but when I try to access this file with AJAX , the readyState is 0 , and I can't get access to the file. here's the code:

JSON file:

{
    "Dogs": [
        {
            "Id": "5435",
            "Name": "ee",
            "Type": "Pudel",
            "Picture": "pudel.jpg"
        },
        {
            "Id": "3321",
            "Name": "Lali",
            "Type": "Labrador",
            "Picture": "labrador.jpg"
        }
    ]
}

JS file with the function that using AJAX:

function show(){
    xhr = new XMLHttpRequest(); 
    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4) { 
    //      alert(xhr.status); it's shows 0 and than alert the error below
            if (xhr.status == 200) { 

                var dataObject = eval("(" + xhr.responseText + ")"); 
                var output = dataObject.Id; 
                output += dataObject.Name; 
                output += dataObject.Type; 
                output += '<img  src='+dataObject.Picture+'width="150" height="150">';
                document.getElementById("result").innerHTML = output; 
            } else { 
                alert("Error! Couldn't get json data"); 
            } 
        } 
    }; 
    xhr.open("GET", "Objects.json", true); 
    xhr.send(); 
}

and the html file:

<html>
    <head>
        <script src="JS.js"></script>
    </head>
    <body>
        <button id="btn" onclick="show()";>Press Me</button>
        <br>
        <div id="result"></div>
    </body>
</html>

Where is the problem?

user2970484
  • 89
  • 1
  • 5
  • 12
  • 2
    Are you sure you can send Jason, could be he doesn't want to be sent ? – adeneo Dec 03 '13 at 00:49
  • Hi, I think so.. after I run the JSON on server (HTTP Preview), if I enter this URL: http://localhost:8080/myProject/Objects.json I see all the objects. – user2970484 Dec 03 '13 at 00:53
  • 1
    @adeneo Next week we'll have a Friday the 13th, then I'm sure we'll be able to send Jason. – bfavaretto Dec 03 '13 at 00:57
  • Wait, is the `readyState` 0 or is the response `status` 0? Your title and question says one thing but your code says another – Phil Dec 03 '13 at 00:58
  • possible duplicate of [XMLHttpRequest status 0 (responseText is empty)](http://stackoverflow.com/questions/5005960/xmlhttprequest-status-0-responsetext-is-empty) – Phil Dec 03 '13 at 01:00
  • the (xhr.status) is 0. sorry my mistake in the title.. – user2970484 Dec 03 '13 at 01:02
  • 3
    you're runninng over `file:///`. Ajax won't run over file, use a `localhost` server (apache, python -m SimpleHTTPserver, node http-server, etc. etc.) – Mike 'Pomax' Kamermans Dec 03 '13 at 01:03
  • @bfavaretto - lol, that was funny! – adeneo Dec 03 '13 at 01:09
  • @Mike'Pomax'Kamermans As indicated in the duplicate question noted above, you can also set Chrome to allow HTTP access via files. I also find Dropbox's *Public* folder a nice test-bed for testing out AJAX requests. – Phil Dec 03 '13 at 02:07
  • you can, although virtually not in OSX. There, a quick python -m SimpleHTTPServer is generally going to be much easier. – Mike 'Pomax' Kamermans Dec 03 '13 at 16:03

0 Answers0