【问题标题】:Prevent defaultHttpResponseTransform to parse JSON like string防止 defaultHttpResponseTransform 解析 JSON 之类的字符串
【发布时间】:2015-06-12 22:52:03
【问题描述】:

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 它时,我得到错误:

语法错误:意外数字 在 Object.parse (本机) 在 fromJson (http://localhost:8080/WebEastGui/build/js/app-vendor.js:41198:14)

有没有办法强制 Angular 不强制解析 JSON 之类的字符串?

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    $http docs 了解Overriding the Default Transformations Per Request

    【讨论】:

    • 感谢您的快速回答。这就是我需要的 siple transformResponse: [function (data) { return data; }] 用默认解析解决我所有的问题。
    猜你喜欢
    • 2016-01-06
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多