我试图从带有javascript的web服务器获取json,但XMLHttoRequest返回0,尽管来自服务器的200 OK响应。 以下是来自wireshark的网络跟踪和来自w3schools的javascript, 可以从以下地址外部访问REST服务:
网络追踪:
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脚本:
<!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>
请告诉我我的设置有什么问题?