AngularJS: get protected JSON with auth headers

398 Views Asked by At

I have a JSON file generated by the server and to access that JSON via URL within the browser I need to log in providing username and password.

Now I am trying to get that JSON with Angular, although can't find working solution. What I already have:

$scope.dane = [];

$http.jsonp('myURLwithUserAndPass?callback=JSON_CALLBACK')
  .success(function(data) {
    console.log('DATA pass');
    $scope.dane = data.products; // response data
  }).error(function() {
    console.log('DATA failed');
  });

This produces errors in the console:

"Uncaught SyntaxError: Unexpected token"

and "Resource interpreted as Script but transferred with MIME type text/json: "myURLwithUserAndPass?callback=angular.callbacks._0"."

I have tried adding auth headers in the config like following:

$httpProvider.defaults.headers.common['Authorization'] = 'Basic ' + myusername + ':' + mypassword;

...but it doesn't help. I've also tried passing username and password in the URL like that:

$http.jsonp('myURLwithUserAndPass?callback=JSON_CALLBACK&userID=SomeUSER&password=SomePSWD ').

Any help on how can I access that JSON?

0

There are 0 best solutions below