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?
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?
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
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;
});
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