How to check HTTP request for OrientDB feature in AngularJS?

I have the following OrientDB function:

http://localhost:2480/function/Application/getPassFailCount/9:600

      

And it returns the following JSON result:

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

      

I need to do to get values "pass"

and "fail"

to use in my web page.

So far, I've been doing this with AngularJS:

$http.get('http://localhost:2480/function/Application/getPassFailCount/9:600').
success(function(data) {
    $scope.data = data.result;



//    $scope.passCount = ;
//    $scope.failCount = ;

});

      

Currently, the error message "401 Unauthorized" appears. How can I verify the authenticity of a request?

And if possible, can anyone provide some advice on how to return the result to passCount

and failCount

from the JSON result?

+3


source to share


1 answer


The OrientDB API documentation states that Basic HTTP authentication must be used to issue commands. This means that you must include the header Authorization

along with your request.

There are several ways to achieve this, here is an easier one. Use the config object parameter for $http.get

to set the request header:

function base64(str) {
    return btoa(unescape(encodeURIComponent(str)));
}

$http.get('http://...', {
    headers: { 'Authorization': 'Basic ' + base64(user + ':' + password) }
}).success(...);

      

Make sure to move all your database logic into the Angular service so you can keep this code in one place and not pollute your controllers.

To make it even cleaner, you can look into $ http interceptors and write a request interceptor that adds a header for every HTTP call.




Regarding the JSON question: you can see that the result object contains an array with one element. Use indexing to get the actual record.

var result = data.result[0];
$scope.passCount = result.pass;
$scope.failCount = result.fail;

      

If you've written the service I mentioned, you can hide this implementation detail from your controller.

function getCount() {
    return $http.get(...).then(function (data) {
        var result = data.result[0];

        // the caller will only see this simpler object
        return { pass: result.pass, fail: result.fail };
    });
}

      

+4


source







All Articles