When trying to query OrientDB from an Angular service method, authentication-related errors are encountered. It appears that two GET requests are required for successful querying of OrientDB.
- An Authentication call: Requesting
with an Authentication headerhttp://localhost:2480/connect/<dbname>
- The Actual Query: Sending a request to
http://localhost:2480/query/<dbname>/sql/<query_text>
Error messages observed in the browser console:
- XMLHttpRequest error when loading http://localhost:2480/connect/atudb. The preflight request's response does not pass the access control check due to the absence of the 'Access-Control-Allow-Origin' header on the requested resource. Access from origin 'http://localhost:8080' is denied.
- GET error at http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName='niteshk'401 (Unauthorized)
- XMLHttpRequest error when accessing http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName='niteshk'. The 'Access-Control-Allow-Origin' header is missing on the requested resource, leading to denial of access from 'http://localhost:8080'. HTTP status code 401 was returned.
The following Angular service code snippet illustrates the issue:
angular.module('ReportApp')
.factory('orientdbService', ['$http', '$log', '$q',
function($http, $log, $q) {
'use strict';
var fac = {};
fac.config = appConfig;
fac.query = function(sql, callback) {
$log.log('Query: '+ sql);
var url = encodeURI(this.config.orientBaseUrl + "query/" + this.config.dbName + "/sql/" + sql);
$log.log('Request URI: '+ url);
connectOrientDb()
.then($http.get(url)
.then(function(response){ //Success
callback(response.data);
},
function(response){ //Failure
callback({
responseStatus: response.status,
responseData: response.data
});
}));
}
fac.fetchCurrentDayReportIdList = function(hostMachineName){
if(typeof(hostMachineName) === "undefined"){
throw {
name: "Parameter Missing",
level: "Show Stopper",
message: "Error detected. This function parameter 'hostMachineName' is mandatory to evaluate return value.",
htmlMessage: "Error detected. This function parameter '<code>hostMachineName</code>' is mandatory to evaluate return value.",
toString: function(){return this.name + ": " + this.message;}
};
}
var sql = "select @rid as rid, runOfDay from TestResult where executorPlatformData.machineName='" + hostMachineName + "'";
var defer = $q.defer();
this.query(sql,
function(data){
defer.resolve(data);
});
return defer.promise;
}
var connectOrientDb = function(){
var url = encodeURI(appConfig.orientBaseUrl + "connect/" + appConfig.dbName);
var req ={
method: 'GET',
url: url,
headers: {
'Authorization': appConfig.authPayload
}
};
$log.log('Request: '+req);
var defer = $q.defer();
$http(req).then(
function(res){ //Success
$log.log('Response: '+res);
defer.resolve(res);
},
function(res){ //Failure
defer.reject(res);
}
);
return defer.promise;
}
return fac;
}
]);
An error occurs when calling fetchCurrentDayReportIdList()
. Assistance with the code correction or implementation reference would be greatly appreciated.
Note:
The global variable
appConfig
stores a JSON object in the following format:
{
"orientBaseUrl": "http://localhost:2480/",
"dbName": "atudb",
"dbUser": "root",
"authPayload": "cm9vdDphZG1pbg==",
"dbPassword": "admin",
"formatDateTime": "yyyy-MM-dd HH:mm:ss"
}