所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问原始“空" [英] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

查看:68
本文介绍了所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问原始“空"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read a csv on my local apache server using Javascript but I get the following error . Below is my code:-

    <!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.那么谁能给我一个替代的解决方案呢? 谁能帮我解决这个问题.

But I get this error 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. I create this csv using a java program . So can anyone give me an alternative solution for this ? Can anyone please help me fix this .

推荐答案

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

Load your HTML file from the HTTP server instead of directly from the disk so the origin will be http://localhost:8080 instead of null.

这篇关于所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问原始“空"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆