others - 在请求的资源上,No 'Access-Control-Allow-Origin'标头 因此,源'http://localhost'不允许访问


function get_last_oxygen(location, start_time, end_time)
 {
 var xmlHttp = null;
 var oxy_url = 'http://localhost:8888/' + location + '/oxygen/1/1/last';
 xmlHttp = new XMLHttpRequest();
 xmlHttp.open("GET", oxy_url, true ); 
 xmlHttp.send( null );
 xmlHttp.onreadystatechange = function() {
 if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
 var json = JSON.parse(xmlHttp.responseText);
 var oxy = document.getElementById('oxygen');
 oxy.innerHTML = json.oxygen;
 get_oxygen_range(location, start_time, end_time);
 //alert(xmlHttp.status + ' ' + xmlHttp.responseText);
 }
 }
 }

 function get_oxygen_range(location, start_time, end_time) {
 var req_range = null;
 var range_url = 'http://localhost:8888/' + location + '/oxygen/1/1/' + start_time + ':' + end_time;;
 req_range = new XMLHttpRequest();
 req_range.open("GET", range_url, true ); 
 req_range.send( null );
 req_range.onreadystatechange = function() {
 if (req_range.readyState == 4) {
 var measurements = eval(req_range.responseText);
 draw_oxygen_diagram(measurements);
 }
 }
 }

 /* Draw a diagram */
 function draw_oxygen_diagram(data) {
 var container = document.getElementById('container');
 //var data = [[1,10], [2,8], [3,11], [4,7], [5,9]];
 graph = Flotr.draw(container, [data], {
 //shadowSize: 0,
 timeUnit: 'second', 
 yaxis : { 
 max: 20,
 min: 0
 },
 xaxis : {
 mode: 'time'
 }
 });
 }

 function refresh() {
 var now = Math.round(new Date().getTime() / 1000);
 now = 1406906881;
 get_last_oxygen('rauco', now - 86400, now);
 setTimeout(refresh, 5000);
 }

 refresh();

时间:

对于一个PHP服务器,你需要做的就是添加这行。


header("Access-Control-Allow-Origin: *");

请确保在发送其他内容之前设置,把它放在顶部应该有用。

Access-Control-Allow-Origin是在服务器的响应上设置的,而不是在客户机请求上设置。

...