1

I have a situation where the $http reponseType is dependent on url query parameter. When output-target=html and if I don't mention responseType at all, I am able to get the response in html properly. However when output-target=xls, I need to do this, to make it work and have responseType set to 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
                })

I need something which will accept all kind of response types. If that option is not available, what is the way around?

I also tried passing function to responseType but that didn't work either.

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

responseType documentation

Community
  • 1
  • 1

2 Answers2

1

What we've done in the project is to have a wrapper over $http called "ajax" and in this service we provide extra parameters to get/post/call/put type of calls and enhance the headers with Content-Type (in your case) just for that particular request. The service provides a default of course (the most common ajax calls headers).

Dragos Rusu
  • 1,508
  • 14
  • 14
0

I have kept the responseType to be arraybuffer and converted the buffer to string when the output target = html.

var decodedString = String.fromCharCode.apply(null, new Uint8Array(buf));