0

I am trying to get json from a web server with javascript but XMLHttoRequest returns 0 despite 200 OK response from the server. Below is network trace from wireshark and the javascript from w3schools, the REST service is externally accessible at this address:

http://ogy.noip.me:5678/home

network trace:

GET /home HTTP/1.1
Host: 192.168.0.240:6666
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://www.w3schools.com/json/tryit.asp?filename=tryjson_http
Origin: http://www.w3schools.com
Connection: keep-alive

HTTP/1.1 200 OK
Content-Type: application/json
Server: HomefrogREST/0.1 HomeAutomation
Content-Length: 2037

{
    "/home/floor1/Bedroom2/Temperature":{
        "units":"C",
        "device-id":"4",
        "units_humanized":"Celsius",
        "rest.timestamp":"2016-06-24 19-47-07",
        "temperature":26.200001,
        "name":"ds1820digitemp"
    },
    "/home/floor1/LivingRoom/Temperature":{
        "units":"C",
        "device-id":"2",
        "units_humanized":"Celsius",
        "rest.timestamp":"2016-06-24 19-47-02",
        "temperature":24.100000,
        "name":"ds1820digitemp"
    },
    "/home/floor1/Garage/Temperature":{
        "units":"C",
        "device-id":"1",
        "units_humanized":"Celsius",
        "rest.timestamp":"2016-06-24 19-47-07",
        "temperature":20.799999,
        "name":"ds1820digitemp"
    },
    "/home/floor1/Bedroom1/Temperature":{
        "units":"C",
        "device-id":"3",
        "units_humanized":"Celsius",
        "rest.timestamp":"2016-06-24 19-47-07",
        "temperature":23.799999,
        "name":"ds1820digitemp"
    },
    "/home/floor1/LivingRoom/AllDevicesOff":{
        "/home/floor1/LivingRoom/Heater1":"no_name",
        "/home/floor1/LivingRoom/Heater2":"no_name",
        "name":"scenectl",
        "rest.timestamp":"2016-06-24 19-45-07"
    },
    "/home/floor1/LivingRoom/Heater1":{
        "device-id":2,
        "status":"on",
        "rest.timestamp":"2016-06-24 19-45-07",
        "name":"telldus"
    },
    "/home/floor1/StorageRoom/Temperature":{
        "units":"C",
        "device-id":"0",
        "units_humanized":"Celsius",
        "rest.timestamp":"2016-06-24 19-47-07",
        "temperature":24.100000,
        "name":"ds1820digitemp"
    },
    "/home/floor1/LivingRoom/Heater2":{
        "device-id":1,
        "status":"off",
        "rest.timestamp":"2016-06-24 19-45-07",
        "name":"telldus"
    }
}

java script:

<!DOCTYPE html>
<html>
<body>

<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://192.168.0.240:6666/home";

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
};
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(arr) {

    document.getElementById("id01").innerHTML = arr;
}
</script>

</body>
</html>

Could you please tell me what is wrong in my setup?

otonchev
  • 1
  • 1
  • Are you on the same ip? Are you going from the same ports? – epascarello Jun 24 '16 at 19:57
  • Open the developer tools in your browser. Look at the console. There will be an error message there. I know what it is because the headers in your request and response show the error state that the browser would complain about. If you had seen the error message you could have copy/pasted it into a search engine and saved yourself the time of writing this question. – Quentin Jun 24 '16 at 19:58
  • @epascarello — No and no. The Origin and Host headers in the request show that. – Quentin Jun 24 '16 at 19:58
  • Well that would be the OPs problem. – epascarello Jun 24 '16 at 19:59

0 Answers0