【发布时间】:2016-12-21 14:43:01
【问题描述】:
我正在尝试从 Angular 服务方法查询 OrientDB。但得到与身份验证相关的错误。 根据我目前的理解,我需要两个 GET 请求才能成功查询 OrientDB。
- 身份验证调用。即使用身份验证标头向
http://localhost:2480/connect/<dbname>请求 - 实际查询。即请求
http://localhost:2480/query/<dbname>/sql/<query_text>
但我在浏览器控制台中收到这些错误:
- XMLHttpRequest 无法加载 http://localhost:2480/connect/atudb。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“http://localhost:8080”。
- GET http://localhost:2480/query/atudb/sql/select%20@rid%20as%20rid,%20runOfDay%20from%20TestResult%20where%20executorPlatformData.machineName='niteshk' 401(未经授权)
- 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"
}
【问题讨论】:
-
您好,您应该启用 CORS stackoverflow.com/questions/27612752/…
标签: javascript angularjs ajax orientdb