Tharik Kanaka
Tharik Kanaka

Reputation: 2510

Cross-Origin Request Blocked in Neo4j API

I am trying to call Neo4j API from jquery.

when i am invoking GET requests it works perfectly

GET request endpoint

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

but when i am invoking POST requests with json body it returns following error.

POST request endpoint

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

Error Message

"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.

When i try from Advance REST Client, it returns correct response. Please refer following code

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


});
});

Upvotes: 0

Views: 2731

Answers (1)

Michael Hunger
Michael Hunger

Reputation: 41706

I have a working example here: http://jexp.github.io/cy2neo

Check out the code: https://github.com/jexp/cy2neo/blob/master/scripts/neo.js#L8

I think the problem was dataType: JSON which caused jquery to send a pre-flight header w/o CORS. I changed it to specifying content-type: JSON

Upvotes: 2

Related Questions