【问题标题】:Cross-Origin Request Blocked in Neo4j APINeo4j API 中的跨域请求被阻止
【发布时间】:2014-10-31 06:23:39
【问题描述】:

我正在尝试从 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 Client 尝试时,它会返回正确的响应。请参考以下代码

$(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 + ')')
        });


});
});

【问题讨论】:

标签: javascript jquery neo4j cors


【解决方案1】:

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

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

我认为问题是 dataType: JSON 导致 jquery 发送一个不带 CORS 的飞行前标头。我将其更改为指定content-type: JSON

【讨论】:

  • 您使用了“transaction/commit”端点,而我使用了“cypher”端点。我检查了您的代码并将我的 Ajax POST 更改为“事务/提交”端点,然后它对我有用。这似乎是密码端点的问题。感谢您的存储库,如果我将您的代码内容用于我的一项研究工作可以吗?
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 2021-06-26
  • 2014-10-13
  • 2019-11-04
  • 2017-02-06
  • 2017-01-04
相关资源
最近更新 更多