0

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


});
});
Tharik Kanaka
  • 2,360
  • 6
  • 29
  • 48

1 Answers1

2

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

Michael Hunger
  • 39,665
  • 3
  • 48
  • 74
  • You have used "transaction/commit" endpoint and i have used "cypher" endpoint. I checked your codes and changed my Ajax POST to "transaction/commit" endpoint, then it worked for me. It seems like a problem of cypher end point. Thank you for repository and Is it ok if i use your code content for one of my research work? – Tharik Kanaka Nov 07 '14 at 08:28