How to access Neo4j dataset through Javascript?

允我心安 提交于 2019-12-12 22:18:50

问题


I am wondering how I get access to my (example)dataset that I have on Neo4j through Javascript, i.e. I have a movie dataset, so I would like to get and receive queries through an local html page? As you might wonder, I am a very beginner to this and I really appreciate it if someone would explain it to me step by step :)

Thanks in advance


回答1:


You can access your Neo4j graph database through the http transactional endpoint

http://neo4j.com/docs/stable/rest-api-transactional.html

and issue cypher queries to query your graph.

As it is a http endpoint, you can access it with normal ajax requests, an e.g. with jquery

var body = JSON.stringify({
                statements: [{
                    statement: 'MATCH (n) RETURN count(n)'
                }]
            });
$.ajax({
            url: "http://localhost:7474",
            type: "POST",
            data: body,
            contentType: "application/json"
        })
            .done(function(result){
                console.log(result);

            })
            .fail(function(error){
                console.log(error.statusText);
            });



回答2:


With a local html page, I assume you mean jquery ?

In general for Javascript there are a number of drivers for neo4j, see http://neo4j.com/developer/javascript

If you want to look for jquery, check out http://jexp.github.io/cy2neo (source: https://github.com/jexp/cy2neo/blob/master/scripts/neo.js#L7)




回答3:


Note you need to add "authentifcation" too, it's done by an ajax function called beforeSend:

beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa("neo4j"+ ":" + "yourNeo4jPassword"));
            }}

also the path to the database is "http://localhost:7474/db/data/transaction/commit" and not "http://localhost:7474/"

So the final Solution is :

$.ajax({
            url: "http://localhost:7474/db/data/transaction/commit",
            type: "POST",
            data: body,
            contentType: "application/json",
            beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa("neo4j"+ ":" + "password"));
            }}
            )
            .done(function(result){
                 console.log(result);
            }) 
            .fail(function(error){
                console.log(error.statusText);
            });


来源:https://stackoverflow.com/questions/27213258/how-to-access-neo4j-dataset-through-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!