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?

1

3 Answers 3

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;
});
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.