Can't access Neo4j REST url on Heroku - heroku

I am trying to use an Ajax call from my web app (not running on Heroku) to query a Neo4j installation on Heroku. The Ajax works flawlessly (no errors, expected JSON response) when connecting to my localhost server url at http://localhost:7474/db/data
but fails when connecting to the Heroku server url at http://login:password#dc02bc2c6.hosted.neo4j.org:7322/db/data
When I say "fail," I mean I receive no errors or results (monitoring in Firebug console). However, I can access the Neo4j webadmin console on Heroku (using Chrome or Firefox but not IE) using the following http://login:password#dc02bc2c6.hosted.neo4j.org:7322/webadmin/
The Ajax code is below.
Can anyone suggest what I am doing wrong and how to connect to Heroku?
restServerURL = "http://login:password#dc02bc2c6.hosted.neo4j.org:7322/db/data";
$.ajax({
type:"POST",
url: restServerURL + "/cypher",
crossDomain: true, //recommended by Neo4j (no effect)
dataType: "json",
data: {
query:"START n = node(*) WHERE n.name =~ '(?i)" + request.term + ".*' RETURN n, n.name ORDER BY LOWER(n.name) ASC LIMIT 12", //"like" (=~) search term, case insensitive ((?i)), show first 12 results
},
success: function( data ) {
//process json here
};
}
});
Thanks,
Jeff

try to use:
xhrFields: {
withCredentials: true
},

Related

Jquery AJAX call requires authentification

I'm trying to use the google chart api in an XPages application.
I'm using the code example given by the documentation : https://developers.google.com/chart/interactive/docs/php_example#exampleusingphphtml-file
I have to replace the call to the php page by a call to an LS agent.
var jsonData = $.ajax({
url: "getData.php",
dataType: "json",
async: false
}).responseText;
So my code goes to :
var jsonData = $.ajax({
url: "http://server/database/agent?openagent",
dataType: "json",
async: false
}).responseText;
On my local domino server, it works fine.
On the production domino server, I get nothing. The chart is not drawn. After debugging the js client side, it seems the ajax call is expecting an authentification even if I had to log in before.
The anonymous access is not allowed on both servers.
The security level seems to be same on both environments
Any help will be welcome (or any other way to proceed if I'm wrong).
Thank you
If you are able to draw the google chart in your local server, but not in production server, this means it is your server issue.
You can add authentication header in your jquery ajax call to make authenticated ajax request
$.ajax({
headers: {
"Authorization": "Bearer <TOKEN HERE>"
}
})
You can also send username and password in jquery ajax call, to make authenticated request. Here is the sample code from the link
$.ajax({
type: 'GET',
url: 'url',
dataType: 'json',
//whatever you need
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', make_base_auth(user, password));
},
success: function () {});
});
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return 'Basic ' + hash;
}
at the end, I tried to run the ajax request through dojo instead of Jquery.
My codes became this one :
var jsonData = dojo.xhrGet({
url: "http://server/database/agent?openagent",
handleAs:"json",
...
})
I did no changes at the security level or anything else.
I do not understand why the jquery syntax is not working as well the dojo syntax.
anyway, it is working now.
Many thanks to all for your suggestions

Plivo SDK call Recording

I am getting an error that i dont understand an cannot find any helpfull informations about:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https:*******' is therefore not allowed access. The response had HTTP status code 401.
function RecordTheCall()
{
var key = '*******************';
$.get( "https://api.plivo.com/v1/Account/"+key+"/Call/?status=live", function( data ) {
var callUuid = data.call_uuid
});
$.ajax({
url: "https://api.plivo.com/v1/Account/"+key+"/Call/"+callUuid+"/Record/",
type: "POST",
data: { 'auth_id': auth_id, 'call_uuid': CallUUID },
dataType: "json",
success: function (res) {
alert(res);
},
error: function(err) {
alert(err);
}
});
}
Call recording cannot be accomplished from the Web SDK directly. You cannot use the Plivo API from your Web browser using Javascript because cross-domain ajax requests are not allowed in browsers for security reasons.
This has been explained in this Wikipedia article. There are some work arounds to overcome this, but it is browser dependent and hence it might not work always. Instead you should use the Plivo XML/API in you application.

$.Ajax and $.Post not working on dev environment

I have a simple function that uses $.Ajax. It's working fine when I deploy the this page to my online website, but on my dev environment it doesn't work, but there is no error. I tried both $.Ajax and $.Post. both not working, but in the F12 profiler tool i see the traffic.
Any Idea?
Thanks.
<script >
function RunAjax()
{
alert("before post");
$.post("http://myshulmgr.com/GetData.asmx/GetEventMembers", { PID: "32", EventID: "8" },
function (data) {
alert("Data Loaded: " + data);
});
$.ajax({
type: "POST",
url: "http://myshulmgr.com/GetData.asmx/GetEventMembers",
//data: {PID: iPID, EventID: iEventID},
data: "{'PID': '" + 32
+ "','EventID': '" + 8 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (jsonRes)
{
alert(jsonRes);
},
failure: function (msg)
{
alert(msg);
}
});
}
Look at your browser's error console. There should be an informative message there.
Your dev environment is probably not on the same domain as the endpoint you are calling, so the same-origin policy is preventing your script from retrieving results from it. Since this is supposed to be a dev environment, you should probably set up a copy of the GetData.asmx script in that environment, rather than calling the production endpoints, and reference it as /GetData.asmx (i.e, as a domain-relative path) in your script.
You cannot perform cross-domain request because of same origin policy.
In your console you can see that this is actually the source of your problem:
console says (Aborted), giving a CORS preflight initiatior, i.e you tried to perform an cross-domain request, but it was aborted due to security reasons.
The reason why it works on production and not on dev is because probably your production environment is in the same domain (myshulmgr.com) to which the request is addressed, while dev environment is not.

Ajax not fetching data but Wget getting the same

I am quering a REST api hosted at amazon servers from my local system. I have shut out the cross domain origin restriction in Google chrome while firing the ajax.
Yet the ajax call fails.
I tried to wget the same URL and also tried file_get_contents() of PHP and received the required JSON object.
Is it possible that at the amazon servers, they have restricted all the ajax calls as in the Google Network tab i can see that the ajax call fails(status).
CODE::
$.ajax({
url :"URL to ec2",
type : 'get',
dataType: "jsonp",
contentType: 'application/json',
success: function (result) {
console.log("OOHKK ::" + result);
},
error : function (xhr, desc, err) {
console.log(xhr);
console.log("Desc: " + desc + "\nErr:" + err);
}
});

phonegap api call not working

I have a rails api running on localhost:3000. On my phonegap app i'm trying to login via api , but it give an error("error") and don't reach to the api.
could be some problem with cross domain request, but is said that in the mobile there is no such thing. internet is working .
my ajax:
$.ajax({
url: 'localhost:3000/api/v1/login',
type: "POST",
dataType: "json",
data : {username:"demo",password:"demo"},
success: function(data) {
navigator.notification.alert("ok");
},
error: function(xhr, textStatus, errorThrown) {
console.log("Error status " + status);
}
});
EDIT 2 - solved
You have to to use local ip eg. 192.168.2.2
and add the host in the Cordova.plist in the ExternalHosts key.
It is because you are using "localhost" when you run this code on a device/emulator then "localhost" refers to the device/emulator itself and not the web server that contains your rails API. You'll need to switch "localhost" to the IP address or hostname of your server.

Resources