从浏览器访问ArangoDB数据

时间:2016-12-18 15:25:07

标签: javascript node.js browser client arangodb

我想从arangoDB访问JSON数据,这是我从浏览器(自己的界面)本地安装的。我收到“401 Unauthorized”请求,我该如何解决?

function getRequest(){ 
    alert("test")
    var request = new XMLHttpRequest();
    request.open("GET","http://root@localhost:8529/_db/_system/_api/document/FC_ACTUAL_SALES/945545",true);
    request.setRequestHeader('Content-Type','application/json');
    request.setRequestHeader('Access-Control-Allow-Credential','true');
    request.responseType = 'json';
    request.addEventListener('load', function(event) {    
        if (request.status >= 200 && request.status < 300) {
            console.log(request.responseText);    
        } else {
            console.warn(request.statusText, request.responseText);    
        }
    });
    request.send();
}

1 个答案:

答案 0 :(得分:1)

您没有使用基本身份验证。

将您的功能更改为:

function getRequest(){ 
    var request = new XMLHttpRequest();
    var user = "root";
    var pass = "";
    request.open("GET","http://root@localhost:8529/_db/_system/_api/document/FC_ACTUAL_SALES/945545",true);
    //Use Basic authentication
    request.setRequestHeader("Authorization", "Basic " + btoa(user + ":" + pass)); 
    request.setRequestHeader('Content-Type','application/json');
    request.setRequestHeader('Access-Control-Allow-Credential','true');
    //request.responseType = 'json'; <--Notice it was removed
    request.addEventListener('load', function(event) {    
        if (request.status >= 200 && request.status < 300) {
            console.log(request.responseText);    
        } else {
            console.warn(request.statusText, request.responseText);    
        }
    });
    request.send();
}