Unauthorized to query neo4j with Ajax

I have created a geohash graph for NYC Cabs data, now the next step is to plot it within a map, for that i choosed to use Leaflet as a javascript mapping library.

my problem is getting data from Neo4j database to JavaScript code.

i found in stackoverflow that you have answered a smilar question by using ajax :

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

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

however this solution doesn't work for me since i get "unauthorized" in console when i run it.
i understand that i need to pass authentification in the url, so how to do that ?

You have to provide the auth to the database too, e.g. by the user
Or if it's readonly you can also hardcode the RO user.

Better to use the javascript bolt driver which handles that for you.

For the http API you have to figure out how to do that with jquery, see here:

Thanks @michael.hunger for you reply ,that helped me to find the solution:
the magic of authentification need to be passed in header with ajax function beforeSend :

 var body = JSON.stringify({
            statements: [{
                statement: 'MATCH (n) RETURN n.geohash'
                }]
            });

		$.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"+ ":" + "yourneo4jpassword"));
			}}
			)
			.done(function(result){
                 console.log(result);
            }) 
            .fail(function(error){
                console.log(error.statusText);
            });

hope that help someone in the future :)

1 Like