How to access Neo4j dataset through Javascript? - javascript

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

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

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)

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

Related

Google GMail API Error: No label add or removes specified

I'm building a small personal web app using the google API, but I'm having an issue with this endpoint: https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify
I'm trying to invoke this through AJAX, but it keeps telling me:
No label add or removes specified
I feel like I'm following the documentation specifications here, and the error isn't giving me any more information; I'm not sure what to do.
I've also tried adding the addLabelIds argument with no data, but it doesn't change anything.
I've tried looking this issue up but all of the solutions seem to relate to quirks of other API implementations and don't really seem to apply to my situation. Any help would be appreciated; it's pretty basic, but here's my AJAX requests code:
$.ajax({
url: "https://gmail.googleapis.com/gmail/v1/users/me/messages/" + messageId + "/modify",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + mailToken);
},
method: "POST",
data: {
"removeLabelIds": ["UNREAD"]
},
success: function(data) {
ele.removeClass("unread");
},
fail: function(data) {
console.log(data);
}
});
Okay, I found the solution myself; I'm just going to post it here in-case anyone else is having this issue.
The issue was that you have to make the request using Content-Type: application/json, and POST the request as a JSON string.
Here's the modified AJAX request that worked.
$.ajax({
url: "https://content-gmail.googleapis.com/gmail/v1/users/me/messages/" + messageId + "/modify?alt=json",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + mailToken);
xhr.setRequestHeader("Content-Type", "application/json");
},
method: "POST",
data: JSON.stringify({
"removeLabelIds": [
"UNREAD"
]
}),
success: function(data) {
ele.removeClass("unread");
},
fail: function(data) {
console.log(data);
}
});

Access JSON with JQuery which has authentication

I want to receive json from this API but it has authentication which username(nama pengguna) is "foo" and password(sandi) "foo". How I can receive it in my program? I use Jquery and eant build it with phonegap and build for Android
I am trying like this
$.ajax({
url: 'http://api.tabloidnova.com/1/subscribe/get?api_key=259225f04f4015746b03e1bad6238eaa&format=json&channel_id=110',
beforeSend: function(xhr){
xhr.setRequestHeader("Authorization","Basic foo:foo");
},
dataType: 'json',
async: false,
success: function(data){
$.each(data,function(i,grab){
console.log(grab[i].article.node_id);
console.log("tes");
})
}
})
but in my ripple emulator it still give me unauthorized error. If I use without ripple emulator it can't work because I run it from localhost which has origin policy. Any suggestion?
SOLVED now. Finally I encode it my username and password first. Thanks
You will need to set the appropriate request header to pass the credentials. See for example here.
$.getJSON({
'url': 'http://host.com/action/',
'otherSettings': 'othervalues',
'beforeSend': function(xhr) {
//May need to use "Authorization" instead
xhr.setRequestHeader("Authentication",
"Basic " + encodeBase64(username + ":" + password)
},
sucess: function(result) {
alert('Done');
}
});
In javascript, with ajax we can do this way:
$.ajax({
'url': 'http://host.com/action/',
'beforeSend': function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + encodeBase64(username + ":" + password) //May need to use "Authorization" instead
},
sucess: function(result) {
alert('done');
}
});
Check the documentation for jQuery.ajax(). You will have more options to implement this.

setting the request header content-type with POST

I am trying to send some JSON data to the server but I keep getting a 415 error: Unsupported Media Type. This is my ajax call
$.ajax({
type: "POST",
url: 'http://mywebsite.com?'+"token="+token+"&account="+account+"&version=1.0&method=put",
dataType: 'jsonp',
contentType: "text/json",
processData: false,data: JSON.stringify(jsonData),
success: function () {
alert("Thanks!");
}
})
}
I noticed that in the request header there is no content-type listed. So how do I set the content type for the request header?
Thanks!
I think that your url is corrupted, you missed / and ?:
url: 'http://www.mywebsite.com/?'+"token="+token+"&account="+account+"&version=1.0&method=put",
Moreover you shuldn't use global urls (with http), because they are blocked by browser...
url: "?token="+token+"&account="+account+"&version=1.0&method=put",
You cannot post data this way outside url. Try following code
$.getJSON("http://www.yourwebsite.com/PersonCount.aspx?id=" + id + "&dt=" + dt + "&t=" + time + "&callback=?", function(data) {
/// Your response from {data}
});

how to parse json result from sharepoint to show list names

This is the first time I work with JSon, so pls dont be rude with me :)
I have this website.
http://msdn.microsoft.com/en-us/library/jj164022(v=office.15).aspx
and this javascript sample:
jQuery.ajax({
url: http:// site url/_api/web/lists,
type: "GET",
headers: {
"ACCEPT","application/json;odata=verbose",
"Authorization", "Bearer " + accessToken
},
})
The thing is I have a div called results and I would like to show the list names that the rest service returns me.
See jQuery official documentation:
http://api.jquery.com/jQuery.ajax/
There are a lot of examples.
EDIT
If your call return one serializable object you can do something like this:
$.ajax({
url: http:// site url/_api/web/lists,
type: "GET",
headers: {
"ACCEPT","application/json;odata=verbose",
"Authorization", "Bearer " + accessToken
},
success: function(data) {
$.each(data, function(index, elem){
//... do some work where ...
alert(elem);
});
}
});
It's hard to say exactly how to show the list in your div without knowing how the returned JSON is formatted. But the main idea is that you'll need to add a success callback function to your jQuery.ajax() call in which you parse the returned data and insert it into your div. For example:
jQuery.ajax({
url: "http://siteurl/_api/web/lists",
type: "GET",
headers: {
"ACCEPT","application/json;odata=verbose",
"Authorization", "Bearer " + accessToken
},
success: function(data) {
var listTitle = data.title; // just an example; not sure if this property exists in your data
$("$myDiv").append('<p>' + listTitle + '</p>');
}
});

Cross Domain AJAX POST / HTTPS / Header Authentication?

I have to issues:
1) I've tried using JsonP, but can't get POSTing to work. Essentially, I'm trying to authenticate with an API, passing a Base64-encoded namevaluepair in the header over HTTPS.
2) How do I pass this key/value in the header? Any help would be appreciated! Here is an example of what I want, though this obviously doesn't work:
// where does this go?
var headerString = 'user=' + encodeURIComponent(username + ':' + password);
$.ajax({
type: "POST",
url: "https://anotherurl.on.another.server/LOGIN",
data: "I have no data, I'm logging in with header authentication",
dataType: "json",
success: function(data) {
},
error: function(data){
}
});
add headers to ajax call:
var headerObj = {'user': encodeURIComponent(username + ':' + password)};
$.ajax({
type: "GET",
url: "https://anotherurl.on.another.server/LOGIN",
data: "I have no data, I'm logging in with header authentication",
dataType: "json",
headers: headerObj,
success: function(data) {
},
error: function(data){
}
});
The best way to do this is probably through a server side proxy on your own domain.
See this page for tips.
This way you will be able to get the response from the other server

Categories