【问题标题】:How can I stop Angular $http from deserializing the response body?如何阻止 Angular $http 反序列化响应正文?
【发布时间】:2014-08-08 21:02:34
【问题描述】:

这个:

$http({method: 'GET', url: '/some/url/returning/json').
        success(function(data, status, headers, config) {
            console.log(data);
        });

表明 Angular 给了我一个 JavaScript 对象作为我的success 处理程序的第一个参数。据推测,它试图通过嗅探响应的内容类型来变得聪明。

这是不受欢迎的行为。如何告诉 Angular 将响应正文作为字符串提供给我?

【问题讨论】:

    标签: angularjs http content-type


    【解决方案1】:

    不良行为是由 Angular 的 transformResponse 引起的。要关闭此功能,请执行以下操作:

    $http({method: 'GET', url: '/some/url/returning/json', transformResponse: [] }).
            success(function(data, status, headers, config) {
                console.log(data);
            });
    

    【讨论】:

    • 感谢您分享您的答案!
    【解决方案2】:

    docs 中提到可以使用responseType 配置选项,大概为:

    $http({method: 'GET', url: '/some/url/returning/json', responseType: 'text'})...
    

    【讨论】:

    • 很遗憾没有;这没有任何作用。 (而且文档很垃圾——它没有说明它的用途。)
    • 别担心——我找到了答案。是transformResponse 在做坏事。不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多