如何在 Angular 应用程序中使用 OrientDB HTTP API?

How to use OrientDB HTTP API in Angular application?

我正在尝试从 Angular 服务方法查询 OrientDB。但是收到与身份验证相关的错误。 按照我目前的理解,我需要两次GET请求才能成功查询OrientDB。

  1. 身份验证调用。即请求 http://localhost:2480/connect/<dbname> 身份验证 header
  2. 实际查询。即请求 http://localhost:2480/query/<dbname>/sql/<query_text>

但是我在浏览器控制台中收到这些错误:

  1. XMLHttpRequest 无法加载 http://localhost:2480/connect/atudb. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' 因此不允许访问。
  2. GET http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName='niteshk' 401(未授权)
  3. XMLHttpRequest 无法加载 http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName='niteshk'。请求的资源上不存在 'Access-Control-Allow-Origin' header。因此不允许访问来源'http://localhost:8080'。 响应的 HTTP 状态代码为 401。

下面是Angular服务的代码

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 '<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
                            /*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;

        }
    ]);

调用 fetchCurrentDayReportIdList() 时出现错误。 任何人都可以指导我参考实现或指出我的代码有什么问题吗?

Note:

The global variable appConfigholds a JSON object of below format.

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

appConfig 尝试在 authPayLoad

中添加 Basic

"authPayload": "Basic cm9vdDphZG1pbg=="

当然,像@wolf4ood 上面说的那样启用 CORS。