0

Simple AngularJS script with user:passw and server.ip

<!DOCTYPE html>
<html >

<script src= "angularjs/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<p>{{ names }}</p>
</div>
<script>
  var app = angular.module('myApp', []);
  app.controller('customersCtrl', ['$scope','$http', function($scope,$http) {
  $http.get('http://user:passwd@server.ip:5984/dbp/3c9f8c470a4a40d81d43467346000010')
.success(function (data) {
  $scope.names = data.rows;
});

}]);
</script>
</body>
</html>

If I use url & put in address bar in my browser, everything works fine

http://user:passwd@server.ip:5984/dbp/3c9f8c470a4a40d81d43467346000010

I mean I get json data.

When I use previous script I get

Remote Address:server.ip:5984
Request URL:http://server.ip:5984/dbp/3c9f8c470a4a40d81d43467346000010
Request Method:GET
Status Code:401 Unauthorized

How can I get json data from CouchDB with authorization header in AngularJS?

CouchDB & AngularJS are on the same server!

I read all 111 q/a (AngularJS CouchDB) from stackoverflow and I didn't find right answer.

I have enable CORS!

Ignjatije
  • 126
  • 7
  • while getting data using `$http.get` what error in console? – Pankaj Parkar May 13 '15 at 11:34
  • error: "unauthorized" reason: "Authentication required." – Ignjatije May 13 '15 at 11:40
  • then you need to set header using `$http({method: 'GET', url: 'http://user:passwd@server.ip:5984/dbp/3c9f8c470a4a40d81d43467346000010i', headers: { 'Authorization': 'Basic '+ someValueHere} });` look at this http://stackoverflow.com/questions/11876777/set-http-header-for-one-request – Pankaj Parkar May 13 '15 at 11:43

1 Answers1

0

You have to send the username/password pair in an Authorization header. (as @pankajparkar has answered correctly before)

The reason why it simply works in the browser address bar is that the browser does this step automatically for you.

Ingo Radatz
  • 1,225
  • 8
  • 9