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,490
  • 6
  • 31
  • 54
  • http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – Ruby Racer Oct 31 '14 at 07:32
  • 1
    @RubyRacer My code is also same whereas i have made crossDomain true – Tharik Kanaka Oct 31 '14 at 07:41
  • Different certs used for the HTTPS and the BOLT connection can cause this. In case someone else comes across this running with TLS enabled on localhost. Open the browser console and if you see a CORS error `Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://localhost:7687/...` you might be able to workaround the issue by browsing to the 'remote resource' in the error, [https://localhost:7687/](https://localhost:7687/). Then accept the BOLT certificate if you trust it (Be sure it's the right one though!). – Charm000 Apr 30 '23 at 14:21

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
  • 41,339
  • 3
  • 57
  • 80
  • 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