No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' null'因此不允许访问

时间:2014-12-26 11:10:39

标签: javascript csv

我正在尝试使用Javascript在我的本地apache服务器上读取csv,但是我收到以下错误。 以下是我的代码: -

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Circles</title>
    <style>
    html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
    }
    </style>

    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
    function csvToJS(csv) {
        var resp=[];
        var rows = csv.split('\n');
         for(var i=0;i<rows.length;i++){
             var row=rows[i].split(',');
            //code
            resp[i]=row;
         }
         return resp;
     }
    var citymap;
    var req = new XMLHttpRequest();
    var file = "http://localhost:8080/example/data.csv";
    req.open('GET', file, true);
    req.send();
    req.onreadystatechange = function() {
        if (req.status == 200) {
           var csv = req.responseText;
          var data = csvToJS(csv);
          console.log(data);

           citymap = data;

        }
        initialize();

    };

        var cityCircle;

        function initialize() {
           //Code here////

            }
        }


    </script>
    </head>
    <body>
        <div id="map-canvas"></div>
    </body>
    </html>

但我收到此错误XMLHttpRequest cannot load http://localhost:8080/example/data.csv. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 我使用java程序创建这个csv。那么有人可以给我一个替代解决方案吗? 任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

从HTTP服务器而不是直接从磁盘加载HTML文件,因此原点将是http://localhost:8080而不是null