【问题标题】:How to use OrientDB HTTP API in Angular application?如何在 Angular 应用程序中使用 OrientDB HTTP API?
【发布时间】:2016-12-21 14:43:01
【问题描述】:

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

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

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

  1. XMLHttpRequest 无法加载 http://localhost:2480/connect/atudb。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-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”标头。因此,Origin '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() 时出现错误。 谁能指导我参考实现或指出我的代码有什么问题?

注意:

全局变量appConfig持有以下格式的JSON对象。

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

【问题讨论】:

标签: javascript angularjs ajax orientdb


【解决方案1】:

appConfig 中尝试在 authPayLoad 中添加Basic

"authPayload": "Basic cm9vdDphZG1pbg=="

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

【讨论】:

  • 感谢您指出此错误@ivan-mainetti。我能够获得连接请求的成功响应 204,但随后的查询调用仍然失败,状态为 401 未授权。有什么想法吗?
  • 即使有查询,您也需要发送授权标头
  • 是的,@ivan。发送带有所有请求的授权标头(基于身份验证配置)解决了问题。
猜你喜欢
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2018-04-07
相关资源
最近更新 更多