在Neo4j API中阻止了交叉原始请求

Cross-Origin Request Blocked in Neo4j API

本文关键字:原始 请求 Neo4j API      更新时间:2023-09-26

我正在尝试从jquery调用Neo4j API。

当我调用GET请求时,它可以完美地运行

GET请求端点

http://localhost:7474/db/data/node/10

但当我用json主体调用POST请求时,它会返回以下错误。

POST请求端点

http://localhost:7474/db/data/cypher

错误消息

"NetworkError: 500 Server Error - http://localhost:7474/db/data/cypher"
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:7474/db/data/cypher. This can be fixed by moving the resource to the same domain or enabling CORS.

当我从Advance REST客户端尝试时,它会返回正确的响应。请参考以下代码

$(document).ready(function(){
  $("#btnQuery").click(function(){
        var Request = "{'query' : 'MATCH (Movie { name:{searchName} })-[SHOWS]-(Cinema) RETURN Cinema','params' : {'searchName' : 'Rio 2'}}";
        //url = mainURL +"cypher";
        url = "http://localhost:7474/db/data/cypher";
        $.ajax({
        url: url,
        headers : {
                      'Authorization' : 'Bearer 5f0e0d8c2a5477d4a8e79fa2d34f84a'
                    },
        crossDomain: true,
        type: 'POST',
        dataType: 'application/json',
        complete: function(xhr) {
            if (xhr.readyState == 4) {
                if (xhr.status == 201) {
                    alert("Data is loaded");
                    clearUsers();
                    isUserAdd = false;
                }
            } else {
                alert("Data is not loaded");
            }
        },
        beforeSend: function (xhr) {
        xhr.setRequestHeader("accept", "application/json");
        xhr.setRequestHeader("Content-Type", "application/json");
        },
            data: ('(' + Request + ')')
        });

});
});

这里有一个工作示例:http://jexp.github.io/cy2neo

查看代码:https://github.com/jexp/cy2neo/blob/master/scripts/neo.js#L8

我认为问题出在dataType: JSON上,它导致jquery发送了一个没有CORS的飞行前标题。我将其更改为指定content-type: JSON