问题
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