0

I have a angular js controller with a http get. I need get response value of http. In headers I send:

-H "Content-Type: application/json" -H "Authorization: Basic YGDJSDJH4564363YTESNSBS4="

The response is a simple text: eg. 'va'

my code:

$http({
            
  method: 'GET',
  url: 'some_url',
  headers: {'Authorization': 'Basic '+tenant_auth }
  }).then(function successCallback(response) {
            console.log(response);
  }, function errorCallback(response) {
            $scope.page_error = response;
            $timeout(function () {
                $scope.page_error = '';
            }, 10000);
        });

The request return ok, but the error is when I try to print the response.

angular.min.js:117 SyntaxError: Unexpected token v in JSON at position 0
at Object.parse (native)
at uc (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:17:36)
at ac (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:91:229)
at https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:92:143
at q (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:7:355)
at fd (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:92:125)
at c (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:93:373)
at https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:130:226
at n.$eval (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:144:467)
at n.$digest (https://mltest.sorbasoft.net/machinelearner/js/angular.min.js:142:47)
maikelm
  • 403
  • 6
  • 30

2 Answers2

0

Since the content type is application/json angular will try to parse it. Try sending text/plain instead.

T J
  • 42,762
  • 13
  • 83
  • 138
  • I tryed, pass 'content-type': 'text/plain' in headers. but the api return the same error – maikelm May 30 '16 at 16:40
  • The answer here: http://stackoverflow.com/questions/27765309/angular-http-service-force-not-parsing-response-to-json – maikelm May 30 '16 at 16:47
-1

In addition to other answers, You always able to use $.ajax or jQuery

In angular the default is JSON. in jQuery the default is text

$.ajax({
  url: 'some_url',
  headers: {'Authorization': 'Basic '+tenant_auth }
  }).then(function successCallback(data) {
            console.log(data);
            $scope.$apply() // Do not forgot to do this every time you change scope outside of angular functions
  }, function errorCallback(data) {
            $scope.page_error = data;
            $timeout(function () {
                $scope.page_error = '';
                $scope.$apply() // Do not forgot to do this when you use jQuery ajax.
            }, 10000);
            $scope.$apply()
        });
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117