1

I am using get API to request data from a specified resource.

$http.get('http:...').success(function() {

});

But I am getting error

"SyntaxError: Unexpected token r in JSON at position 1",

How we can handle this issue?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Bhagesh Arora
  • 547
  • 2
  • 12
  • 30
  • The `.success` method had been [deprecated and removed from AngularJS V1.6](https://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Jun 12 '18 at 11:41

3 Answers3

0

Override the default response transformation of JSON.parse

If the Content-Type is application/json or the response looks like JSON, The $http service will deserialize it using a JSON parser.

To avoid that, configure the transformResponse property to pass the response without any transformation:

var config = { transformResponse: angular.identity };

$http.get(url, config).then(function(response) {
    console.log(response.data);
});

For more information, see AngularJS $http Service - Overriding the Default Transformations Per Request

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95
0

The .success and .error methods have been removed from AngularJS 1.6.

Instead of

$http.get(...)
  .success(function(data, ...) {
    ...
  })
  .error(function(data, ...) {
    ...
  });

use

$http.get(...).then(
  function success(response) {
    var data = response.data;
    ...   
},function error(response) {
    var data = response.data;
});
mahan
  • 12,366
  • 5
  • 48
  • 83
-1

Json is the default, you can set the expected reponse type to text:

$http.get('http:...', {responseType: "text"}).success(function(reponse) {
    console.log(reponse);
});

More info can found in AngularJS docs

Michael
  • 896
  • 10
  • 28