Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent defaultHttpResponseTransform to parse JSON like string

Tags:

angularjs

AngularJS defaultHttpResponseTransform tries to figure out if data is json and tries to parse it:

function defaultHttpResponseTransform(data, headers) {
  if (isString(data)) {
    // Strip json vulnerability protection prefix and trim whitespace
    var tempData = data.replace(JSON_PROTECTION_PREFIX, '').trim();

    if (tempData) {
      var contentType = headers('Content-Type');
      if ((contentType && (contentType.indexOf(APPLICATION_JSON) === 0)) || isJsonLike(tempData)) {
        data = fromJson(tempData);
      }
    }
  }

  return data;
}

However data in response in text/plain that is swift message, and looks like JSON but it is not. And when Angular tries to JSON.parse it, I get error:

SyntaxError: Unexpected number at Object.parse (native) at fromJson (http://localhost:8080/WebEastGui/build/js/app-vendor.js:41198:14)

Is there a way to force Angular not to force parsing JSON like string?

like image 673
Jan Chowaniec Avatar asked Oct 20 '22 09:10

Jan Chowaniec


1 Answers

Read about Overriding the Default Transformations Per Request from $http docs

like image 176
dragonfly Avatar answered Oct 22 '22 01:10

dragonfly