防止defaultHttpResponseTransform像字符串一样解析JSON

时间:2015-06-12 14:44:30

标签: angularjs

AngularJS defaultHttpResponseTransform试图找出数据是否为json并尝试解析它:

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;
}

然而,text/plain中的回复数据是swift消息,看起来像JSON,但事实并非如此。当Angular尝试JSON.parse时,我收到错误:

  

SyntaxError:意外的数字       at Object.parse(native)       在fromJson(http://localhost:8080/WebEastGui/build/js/app-vendor.js:41198:14

有没有办法强制Angular不强制解析像字符串一样的JSON?

1 个答案:

答案 0 :(得分:3)

$http docs

了解Overriding the Default Transformations Per Request