Neo4j API中阻止了跨源请求

时间:2014-10-31 06:23:39

标签: javascript jquery neo4j cors

我试图从jquery调用Neo4j API。

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

GET请求端点

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

但是当我用json body调用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 + ')')
        });


});
});

1 个答案:

答案 0 :(得分:2)

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

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

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