httprequest - How to authenticate a HTTP request to an OrientDB function on AngularJS? -


i have following orientdb function:

http://localhost:2480/function/application/getpassfailcount/9:600 

and returns following json result:

{"result":[{"@type":"d","@version":0,"pass":16.0,"fail":2.0,"@fieldtypes":"pass=d,fail=d"}]} 

what need values of "pass" , "fail" use in web page.

so far have done angularjs:

$http.get('http://localhost:2480/function/application/getpassfailcount/9:600'). success(function(data) {     $scope.data = data.result;    //    $scope.passcount = ; //    $scope.failcount = ;  }); 

currently gives error "401 unauthorized". how authenticate request?

and if possible, can give tips on how passcount , failcount json result returned?

the orientdb http api documentation states have use http basic authentication issuing commands. means have include authorization header along request.

there few ways achieve this, here simpler one. use configuration object parameter $http.get set header on request:

function base64(str) {     return btoa(unescape(encodeuricomponent(str))); }  $http.get('http://...', {     headers: { 'authorization': 'basic ' + base64(user + ':' + password) } }).success(...); 

you should move database logic angular service, can keep code in 1 place instead of polluting controllers.

to make cleaner, $http interceptors , write request interceptor adds header every http call.


regarding json question: can see result object contains array single element. use indexing actual record.

var result = data.result[0]; $scope.passcount = result.pass; $scope.failcount = result.fail; 

if wrote service mentioned, hide implementation detail controller.

function getcount() {     return $http.get(...).then(function (data) {         var result = data.result[0];          // caller see simpler object         return { pass: result.pass, fail: result.fail };     }); } 

Comments