Prevent defaultHttpResponseTransform for parsing JSON string

AngularJS defaultHttpResponseTransformtries to find out if the json data is 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, the data in the answer is in text/plain, i.e. swift, and look like JSON, but it’s not. And when Angular tries JSON.parse, I get an error:

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

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

+4
source share
1 answer

Read about Overriding the Default Transformations Per Requestfrom $ http docs

+3
source

All Articles