Finding the Angular alternative for $http $resource when making a PDF GET request

In my Angular project, I utilize $resource for all the requests to the Web API. However, I recently encountered an issue with handling data from a request for a PDF file. After some research, I came across a snippet that worked perfectly using $http. Despite trying to achieve the same result using $resource, I have been unsuccessful:

        $http.get('/api/pdf/shipper', 

        {responseType:'arraybuffer'})
          .success(function (response) {
             var file = new Blob([(response)], {type: 'application/pdf'});
             var fileURL = URL.createObjectURL(file);
             $scope.content = $sce.trustAsResourceUrl(fileURL);
    });

It worked flawlessly with the help of the $sce service to validate the URL and assign it to $scope.content for display in a pop-up window. However, when I tried to implement the same functionality using $resource as part of a service for all the page's requests, I encountered a problem:

        InvoicePDF: $resource('/api/pdf/invoice', {},  {  
            method: 'GET',
            headers: {
                accept: 'application/pdf'
            },
            responseType: 'arraybuffer',
            cache: true,
            transformResponse: function (data) {
                var pdf;
                if (data) {
                    pdf = new Blob([data], {
                        type: 'application/pdf'
                    });
                }
                return {
                    response: pdf
                };
            }
        })

Subsequently, when calling this from the controller:

            SAJAPdata.InvoicePDF.get().$promise.then(function(pdf) {

            $scope.content = $sce.trustAsResourceUrl(pdf);

        });

I faced an issue where Angular complained about [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: resourceUrl.

Any suggestions or insights would be greatly appreciated.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The operation to remove the child element could not be completed

var first_content = document.getElementById('first-content'), offered_games = document.getElementById('offered-games'); for(var i = 0, e = offered_games.children; i < e.length; i++) { e[i].onmouseenter = function() { var ...

Tips for updating values in a nested array within JSON

I am working with the following .json file and my goal is to update the values of "down" and "up" based on user input. "android": { "appium:autoAcceptAlerts": true, "appium:automationName": "UiAutomator2", ...

Angular JS: using data binding with dynamically inserted html elements

I'm currently experiencing difficulties with two-way data binding in AngularJS. I have provided a sample code snippet below: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular ...

The JQuery functionality is failing to execute properly on Internet Explorer

I have developed a JQuery script that appears to be working perfectly in all browsers, except for IE 8. Interestingly, when I checked Internet Explorer's error details, it did not provide any specific information about the issue. Instead, IE simply po ...

Navigating on Blogger can be a tricky task when it comes to searching and

I recently added a Table to my blogger post, along with an input form for readers to easily search data. After finding the code on W3Schools (link here), I implemented it successfully. However, I am looking to make some improvements. Here is my question: ...

Is there a way to apply the same class exclusively to the first child within a group?

How can I include specific classes for only the initial group of items in a nested ul li list? <ul> <li class="first-group"> <ul> <li></li> </ul> </li> <li class="first-group"></li> & ...

establish connections within dynamic data table entries

As a newcomer to Javascript, I've been struggling with a particular question for some time now. The challenge at hand involves a dynamic jQuery table where I aim to hyperlink one row of the table data to a .php page in order to perform certain querie ...

Trigger function when the element is generated

Is there a way to execute a function only once while still having it run each time a new element is created using v-for? <div v-for"value in values"> <div @ function(value, domElement) if value.bool===true @> </div> ...

Submitting data using JavaScript's POST method

I am facing a challenge with posting Array data to an HTTP connector. My data is structured as follows: var data = [{ key:'myKey', keyName:'myKeyName', value:'value', valueName:'valueName' }, { ...

How can you transform square bracket object keys from a URL address into a nested object using Javascript?

Considering the following: var obj = { "object[foo][bar][ya]": 100 }; Is there a way to achieve this structure: var obj = { object: { foo: { bar: { ya: 100 }}}}; ...

Having trouble saving user input from a form to a database using axios, mongodb, and vue?

I am a beginner in working with Vue and I'm currently facing an issue while trying to submit user input data to my MongoDB using axios. Although the data from the database is displayed on the page, I can't seem to get the form input data to succe ...

Is the CSS Transition Solely Active for the Introductory Animation?

I'm currently looking to enhance the smoothness of div expansion and contraction on hover using CSS transitions. However, I have noticed that the Transition property only seems to affect the entry animation (i.e., when the mouse hovers and the div exp ...

Can $.ajax be used as a replacement for $(document).ready(function()?

After conducting an extensive search, I am still unable to find a clear answer to my assumption. The code I used is as follows: <?php session_start(); if (isset($_SESSION['valid_user']) && $_SESSION['from']==1) { ?> ...

Issue with connecting to server causing difficulty in downloading files using AngularJS

The data fetched from the server is displayed here. viewApp.controller('perInfor',['$scope','$http',function($scope,$http){ $scope.viewPerson = function(id){ console.log(id); $http({ method:'get', ...

Press the Text and Alter Text with React

I'm having an issue with the onClick event using React hooks. My goal is to have the text change to a different one when the user clicks, and then revert back to the original text on another click. For example, clicking "Change First" should switch it ...

Tips for implementing a CSS loader exclusively on a particular section of your content

Is it possible to apply a loader image to a specific section of content on a webpage? All the tutorials I've come across for loader images focus on applying them to entire webpages. However, I am looking to implement a simple loader only to a specifi ...

Problems with Firefox and Angular-indexedDB

Incorporating AngularJS and IndexedDB into my application has been quite the challenge. Fortunately, I stumbled upon a valuable resource on GitHub that features an AngularJS module specifically designed for working with IndexedDB. This library goes by the ...

Moving Iframe Data to a div

Is there a way to easily copy content from a specific iframe on my index.aspx page, open a new window, and paste it into a div? I currently have this code (not mine) but I would like a cleaner solution: $('#ifContent').clone().appendTo(w.docume ...

Setting up Angularjs on your computer with the help of bower

In my home country, certain social networking websites and other online platforms are restricted. For instance, I am unable to access sites like meteor.com, codecademy.com, bower.herokuapp.com, and more. When attempting to install packages by running the & ...

Choose an image to be displayed at either full width or full height, depending on which dimension is reached first

I have a query regarding resizing an image within a div to either 100% width or 100% height of the containing div. Despite setting max-height and max-width to 100% as recommended here, I still encounter overflow issues without wanting to crop the image usi ...