How to use OrientDB HTTP API in Angular application?
<p>I am trying to query OrientDB from an Angular service method. But getting authentication related error.
As per my current understanding, I need two GET requests to query OrientDB successfully.</p>

<ol>
<li>Authentication call. i.e. Request to <code>http://localhost:2480/connect/&lt;dbname&gt;</code> with Authentication header</li>
<li>Actual Query. i.e. Request to <code>http://localhost:2480/query/&lt;dbname&gt;/sql/&lt;query_text&gt;</code></li>
</ol>

<p>But I am getting these errors in browser console:</p>

<ol>
<li>XMLHttpRequest cannot load <a href="http://localhost:2480/connect/atudb" rel="nofollow noreferrer">http://localhost:2480/connect/atudb</a>. Response to preflight request doesn't pass access control check: <strong>No 'Access-Control-Allow-Origin' header is present on the requested resource.</strong> Origin '<a href="http://localhost:8080" rel="nofollow noreferrer">http://localhost:8080</a>' is therefore not allowed access.</li>
<li>GET <a href="http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName=" rel="nofollow noreferrer">http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName=</a>'niteshk' <strong>401 (Unauthorized)</strong></li>
<li>XMLHttpRequest cannot load <a href="http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName=" rel="nofollow noreferrer">http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName=</a>'niteshk'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:8080" rel="nofollow noreferrer">http://localhost:8080</a>' is therefore not allowed access. <strong>The response had HTTP status code 401.</strong></li>
</ol>

<p>Below is the code for Angular service</p>

<pre><code>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 prameter 'hostMachineName' is mandatory to evaluate return value.", 
                        htmlMessage: "Error detected. This function prameter '&lt;code&gt;hostMachineName&lt;/code&gt;' 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
                            /*throw {
                                name:   "Connection Failed",
                                level:  "Show Stopper",
                                message: "Error detected ("+response.status+"). Unable to connect to configured database.",
                                htmlMessage: "Error detected ("+response.status+"). Unable to connect to configured database.",
                                toString:    function(){return this.name + ": " + this.message;}
                            };*/
                            defer.reject(res);
                        }
                    );
                return defer.promise;
            }

            return fac;

        }
    ]);
</code></pre>

<p>I am getting error when <code>fetchCurrentDayReportIdList()</code> is called.
Can anyone please guide me with a reference implementation or point out what is wrong with my code?</p>

<blockquote>
  <p><strong>Note:</strong></p>
  
  <p>The global variable <code>appConfig</code>holds a JSON object of below format.</p>
</blockquote>

<pre><code>    {
        "orientBaseUrl": "http://localhost:2480/",
        "dbName": "atudb",
        "dbUser": "root",
        "authPayload": "cm9vdDphZG1pbg==",
        "dbPassword": "admin",
        "formatDateTime": "yyyy-MM-dd HH:mm:ss"
    }
</code></pre>