SyntaxError: Unexpected token F, when using $http.post and restangular post

Issue

We are using angular in our current project and we are doing API calls with Restangular. Suddenly i started to get error when doing a post to a certain endpoint.

the post was something like:

Restangular.one('aaa').post('bbb', data)
  .then((response) => {
    console.log(response);
  }, (err) => {
    console.log(err);
  });

And the log was this:

SyntaxError: Unexpected token F

After a while I gave up and tried to use angular $http service.

$http({
  method: 'POST',
  url: '/aaa/bbb',
  data: data
}).then(function successCallback(response) {
  console.log(response);
  }, function errorCallback(response) {
    console.log(response);
  });

and the log:

SyntaxError: Unexpected token F

After that i started to get desperate and did a post with jQuery.ajax

jQuery.ajax({
  url:'/aaa/bbb',
  type: 'POST',
  data: data,
  contentType: 'application/json; charset=utf-8',
  success: function(s) {
    console.log(s);
    console.log('jquery success');
  },
  error: function(e) {
    console.log(e);
    console.log('jquery error');
  },
  complete: function(c) {
    console.log(c);
    console.log('jquery complete');
  }
});

And the log when using jquery.ajax:

Object {readyState: 4, responseText: "Fatal error: actual error message here", status: 200, statusText: "OK"}
jquery error

Finally got something! We are using drupal as our backend and one of the modules was not enabled.
I am just wondering why did restangular and $http fail to give proper error message.

Solution

The responses produced by Restangular and the $http module are identical to the response that would be received from the JSON module. For example:

JSON.parse("{F}");

produces the error:

Uncaught SyntaxError: Unexpected token F

The response from jQuery is the raw response data from the server without the application of JSON.parse.

Answered By – gnerkus

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.