简体   繁体   English

所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost”

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access

Hi guys I'm trying to go through this code that draws a graph, but I get No 'Access-Control-Allow-Origin' header is present on the requested resource. 嗨,大家好,我正在尝试遍历绘制图形的代码,但是在请求的资源上没有'Access-Control-Allow-Origin'标头。 Origin 'XXXX' is therefore not allowed access. 因此,不允许访问来源“ XXXX”。 I know there is things like CORS, but I don't know how to use it. 我知道有类似CORS的东西,但是我不知道如何使用它。

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();

For a PHP server all you have to do to enable CORS is to add this line. 对于PHP服务器,启用CORS所需要做的就是添加此行。

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

Make sure this is set before any other content is sent. 发送任何其他内容之前,请确保已设置此设置。 That means above any echo statements or html. 这意味着在任何echo语句或html之上。 Putting it towards the top should work. 将其放在顶部应该起作用。

The website http://enable-cors.org/index.html is a good resource for learning more about CORS and how to allow it on different server setups. 网站http://enable-cors.org/index.html是学习更多有关CORS以及如何在不同服务器设置上允许它的更多信息的好资源。

Refer Angular.js No 'Access-Control-Allow-Origin' header is present on the requested resource. 请参阅Angular.js所请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access 因此,不允许访问原始“空”

"Access-Control-Allow-Origin is set on the response from server, not on client request to allow clients from different origins to have access to the response." “ Access-Control-Allow-Origin是在服务器的响应上设置的,而不是在客户端请求上设置的,以允许来自不同来源的客户端可以访问该响应。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost:4400” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4400' is therefore not allowed access - 请求的资源上没有“Access-Control-Allow-Origin”标头。 因此不允许Origin'http:// localhost:8100'访问 - — No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access Socket.io没有'Access-Control-Allow-Origin'标头出现在请求的资源上。 因此不允许Origin'http:// localhost'访问 - Socket.io No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许Origin'http:// localhost:8100'访问 - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost:4200” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed accesss AngularJS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access Golang 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许访问原点 'null' - Golang No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问源“ file://”。 - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin '...' 因此不允许访问 - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM