Itsik Mauyhas Itsik Mauyhas - 1 year ago 92
AngularJS Question

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

I am sending

request to my server and my response format is
, 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

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

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


Answer Source

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:

  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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download