我遇到的情况是 $http reponseType 依赖于 url 查询参数。当output-target=html并且我根本不提及responseType时,我能够正确获得html中的响应。 但是,当 output-target=xls 时,我需要这样做,才能使其工作 并将 responseType 设置为 arraybuffer ( to get response in excel )

var blob = new Blob([data], {type: "application/vnd.ms-excel"});

 $http({ 
                    method: 'GET', 
                    url: "https://xyz/abc?output-target=" + $scope.outputTarget, 
                    headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': '*/*'}, 
                    responseType: arraybuffer //for $scope.outputTarget=xls 
                }) 

我需要一些可以接受所有类型响应类型的东西。如果该选项不可用,有什么办法呢?

我还尝试将函数传递给responseType,但这也不起作用。

responseType: (function () { 
                        if($scope.outputTarget === "table/excel;page-mode=flow") { 
                            var arraybuffer 
                            return arraybuffer; 
                        } else {return;} 
                    })() 

responseType documentation

请您参考如下方法:

我们在项目中所做的是在 $http 上有一个名为“ajax”的包装器,在此服务中,我们提供额外的参数来获取/发布/调用/放置类型的调用和使用 Content-Type 增强 header (根据您的情况)仅针对该特定请求。当然,该服务提供了默认值(最常见的 ajax 调用 header )。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!