【问题标题】:CORS issue on noe4j with D3.js带有 D3.js 的 noe4j 上的 CORS 问题
【发布时间】:2018-01-09 08:10:48
【问题描述】:

我正在研究 Neo4j 和 D3.js 库。 我需要在我的简单 html 页面上使用 d3.js 显示 neo4j 图。我正在关注这个tutorial。

这是我的代码:

<html>
<head>
<title>Page Title</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script type="text/javascript">
        function post_cypherquery() {
            // while busy, show we're doing something in the messageArea.
            $('#messageArea').html('<h3>(loading)</h3>');

            // get the data from neo4j
            $.ajax({
                url: "http://localhost:7474/db/data/transaction/commit",
                type: 'POST',
                data: JSON.stringify({ "statements": [{ "statement": $('#cypher-in').val() }] }),                
                //dataType:"json",
                accept: 'application/json; charset=UTF-8',
                success: function () { },
                error: function (jqXHR, textStatus, errorThrown) { $('#messageArea').html('<h3>' + textStatus + ' : ' + errorThrown + '</h3>') },
                complete: function () { }
            }).then(function (data) {

                $('#outputArea').html("<p>Query: '"+ $('#cypher-in').val() +"'</p>");
                $('#messageArea').html('');

                var d3_data = [];
                $.each(data.results[0].data, function (k, v) { d3_data.push(v.row); });

                var margin = { top: 40, right: 200, bottom: 40, left: 40 },
                    width = ($(window).width()/2) - margin.left - margin.right,
                    height = ($(window).height()/2) - margin.top - margin.bottom, 
                    barHeight = height / d3_data.length;

                var maxrange = d3.max(d3_data, function (d) { return d[1]; }) + 3;

                var scale_x = d3.scale.linear()
                    .domain([0, maxrange])
                    .rangeRound([0, width]);

                var scale_y = d3.scale.linear()
                    .domain([d3_data.length, 0])
                    .rangeRound([0, height]);
                 var xAxis = d3.svg.axis()
                    .scale(scale_x)
                    .ticks(maxrange)
                    .orient("bottom");

                var yAxis = d3.svg.axis()
                    .scale(scale_y)
                    .ticks(d3_data.length)
                    .orient("left");   

                var chart = d3.select("#outputArea")
                    .append("svg")
                    .attr("width", (width + margin.left + margin.right) + "px")
                    .attr("height", (height + margin.top + margin.bottom) + "px")
                    .attr("version", "1.1") 
                    .attr("preserveAspectRatio", "xMidYMid")
                    .attr("xmlns", "http://www.w3.org/2000/svg");

                chart.append("title")
                    .text("Number of players per movie");

                chart.append("desc")
                    .text("This SVG is a demonstration of the power of Neo4j combined with d3.js.");

                chart = chart.append("g")
                    .attr("transform", "translate(" + (+margin.left) + "," + (+margin.top) + ")");

                chart.append("g")
                    .attr("class", "x axis")
                    .attr("transform", "translate(0," + (+height) + ")")
                    .call(xAxis);

                chart.append("g")
                    .attr("class", "y axis")
                    .attr("transform", "translate(" + (-1) + ",0)")
                    .call(yAxis);

                var bar = chart.selectAll("g.bar")
                    .data(d3_data)
                    .enter().append("g").attr("class","bar")
                    .attr("transform", function (d, i) { return "translate(0," + i * barHeight + ")"; });

                bar.append("rect")
                    .attr("width", function (d) { return scale_x(d[1]) + "px"; }) 
                    .attr("height", (barHeight - 1) + "px" );

                bar.append("text")
                    .attr("class", "info")
                    .attr("x", function (d) { return (scale_x(d[1]) - 3) + "px"; })
                    .attr("y", (barHeight / 2) + "px")
                    .attr("dy", ".35em")
                    .text(function (d) { return 'players: ' + d[1]; });

                bar.append("text")
                    .attr("class","movie")
                    .attr("x", function (d) { return (scale_x(d[1]) + 3) + "px"; })
                    .attr("y", (barHeight / 2) + "px")
                    .attr("dy", ".35em")
                    .text(function (d) { return d[0]; });
            });
        };
    </script>

    <h1>Cypher-test</h1>
<p>
<div id="messageArea"></div>
<p>
<table>
  <tr>
    <td><input name="cypher" id="cypher-in" value="MATCH (n1)-[r]->(n2) RETURN r, n1, n2 LIMIT 25" /></td>
    <td><button name="post cypher" onclick="post_cypherquery();">execute</button></td>
  </tr>
</table>
<p>
<div id="outputArea"></div>
<p>

</body>
</html>

当我运行这段代码时,它给了我 CORS 错误:

XMLHttpRequest 无法加载 http://localhost:7474/db/data/transaction/commit。不 请求中存在“Access-Control-Allow-Origin”标头 资源。因此,Origin 'null' 不允许访问。响应 有 HTTP 状态码 401。

我用谷歌搜索过,但没有发现任何有用的东西。我想我必须进行一些身份验证,但是在哪里以及如何进行?不要将此标记为重复,只是给我一点提示。谢谢

【问题讨论】:

标签: javascript jquery d3.js neo4j cors


【解决方案1】:

在您的 google chrome 浏览器上安装 this extension 并启用它。希望它能解决你的问题。

【讨论】:

  • 几天前我也遇到了同样的问题,这个插件解决了。清除浏览器缓存/cookie 一次。祝你好运。
  • 在我的本地开发环境中遇到了与 d3.v4 相同的问题,这个扩展解决了这个问题。
【解决方案2】:

我收到此错误,并且由于某些原因,我的 chrome CORS 扩展程序无法正常工作。最简单和安全的方法是执行以下操作。

安装简单节点服务器。例如。 http-server 或 live-server 使用 npm。

在工作目录中打开 cmd 并启动服务器。因此,您将拥有服务器中的目录,并且可以访问工作目录中的所有文件而不会被 COR 阻止。对于我是 D3 初学者的我来说,这是访问用于开发的文件的最简单、最安全的方法。

【讨论】:

    猜你喜欢
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多