Itsik Mauyhas Itsik Mauyhas - 4 months ago 22
AngularJS Question

Angular - Unexpected token x in JSON at position 1 - response starts with [

I am sending

$http
request to my server and my response format is
[SERVICE_NAME = XYZ][PRAMA = YZR]
, after the request finished I am getting this error:

SyntexError: Unexpected token S in JSON position 1 at Object.parse


I think it is because the response starts with
[
, anyone jas faced and solved that problem?
Here is my
$http
request:

var promise = $http({method : 'GET', url : baseUrl, headers:{"Content-Type":text/html}, params:queryParams}).then(function(response){
console.log(response);
retuen response.data;
}, function(error){
console.log(error)
})
return promise;
};


I know my
response
is not a vaild
JSON
, still I need to parse it.

Thanks.

Answer

Don't return your response with application/json content type. If your response have a Content-Type header with the value of application/json, Angular will automatically parse the JSON. Return your response with text/plain content type to avoid this.

Another option is to set the response transform function manually, for example:

$http({
  url: '...',
  method: 'GET',
  transformResponse: [function (response) {
      // Data response is plain text at this point
      // So just return it, or do your parsing here
      return data;
  }]
});
Comments