How to extract Response body as either plain text or XML in an AngularJS 2 HTTP GET call

I have a challenge with making a get request to the server that returns XML:

let responseText = "";
this.http.get('http://example.com', {headers : headers})
     .map((res:Response) => res.text()).subscribe(data => responseText = data);

However, the value of the variable responseText remains an empty string. How can I retrieve plain text to then convert it into XML, or is there a way to directly obtain the XML data?

Answer №1

Your solution seems to be on the right track. It looks like the issue might be related to accessing 'text' before the HTTP call has completed. Remember that HTTP calls are asynchronous, so you need to handle them accordingly. Give this updated code a try and you should see the desired result:

let text = "";
this.http.get('https://jsonplaceholder.typicode.com/posts')
.map((res:Response) => res.text())
.subscribe(
    data => {
        text = data;
        console.log(text);
     });

Answer №2

Excellent strategy! Implement it in the following way:

this.fetchData('http://samplewebsite.com', {customHeaders : headers})
     .transform((result:Response) => { return result.content() }).subscribe(resultData =>  {output = resultData});

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

Incorporate Moment library for date selection in AngularDateRangePicker

I have attempted to implement AngularDateRangePicker according to the instructions provided at the following URL: https://www.npmjs.com/package/ngx-daterangepicker-material However, when I include the line selected: {startDate: Moment, endDate: Moment}; ...

Unit testing component in Ionic 2 with Ionic's specific markup and elements

Within my Angular 2 component for an Ionic 2 app, I utilize Ionic's markup as shown below: <ion-card> <h3>{{ rawcontent.name }}</h3> <p *ngIf="rawcontent.description">{{ rawcontent.description }}</p> </ion-car ...

How can I make a button change color dynamically in Angular when using Ionic?

<ion-col col-3> <button ion-button (click)="onPunchPress($event)"><span>1</span></button> </ion-col> <ion-col col-3> <button ion-button (click)="onPunchPre ...

Issue with angular oidc-client library's automaticSilentRenew functionality

I'm currently implementing oidc authentication using "oidc-client" version 1.10.1. Below is the configuration I have set up for the userManager: const settings = { authority: (window as any).__env.auth.authority, //OAuth 2.0 authorization end ...

The jspdf tool tries to cram my extensive data into a single page, resulting in an overcrowded and barely visible PDF document

My PDF generated using Jspdf is being forced to fit in one page, making it difficult to see all the data because there is a huge amount of information present. To view the issue, please visit this link: https://jsfiddle.net/frost000/04qt7gsm/21/ var pdf ...

What is the method for choosing an element by class name in TypeScript?

Currently, I'm working on creating a responsive menu bar that collapses on smaller screens. The challenge I'm facing is that I'm using TypeScript for this project. Is there any guidance on how to translate the following code into TypeScript? ...

`How to utilize the spread operator in Angular 4 to push an object to a specific length`

One issue I'm facing is trying to push an object onto a specific index position in an array, but it's getting pushed to the end of the array instead. this.tradingPartner = new TradingPartnerModel(); this.tradingPartners = [...this.tradingPartner ...

Intellij2016 is issuing warnings for the sass use of the :host selector in Angular2 code

One interesting feature of Angular 2 is the special selector it uses to reference its own component: :host display: block height: 100% !important width: 100% text-align: center position: relative However, Intellij does not provide a way to supp ...

Retrieve a specific attribute from a collection of JSON objects and transfer it to a separate object

Having a JSON object array with various project information: [ {"Project":"Project 1","Domain":"Domain1","Manager":"Manager1"}, {"Project":"Project 2","Domain":&q ...

Sort the elements within the *ngFor loop according to the category upon clicking the button in Angular

Currently, I have a collection of items that I am iterating through using *ngFor. Above this list, there are category buttons available as shown in the HTML snippet below. My goal is to enable filtering of the list based on the category of the button click ...

How can I incorporate MS Teams call recording into my web platform?

Currently in the process of developing a web application using Angular. Successfully integrated video call functionality through Azure Communication. Looking to now incorporate MS Teams call recording feature. Seeking assistance with reference links and s ...

Guide to automatically closing the calendar once a date has been chosen using owl-date-time

Utilizing Angular Date Time Picker to invoke owl-date-time has been functioning flawlessly. However, one issue I have encountered is that the calendar does not automatically close after selecting a date. Instead, I am required to click outside of the cal ...

Can a dynamic HTML page be created using Angular's ngClass directive and Bootstrap classes to ensure responsiveness?

Is there a way to dynamically resize buttons in my Angular application using the Bootstrap class btn-sm? I'm currently using this code snippet: <button [ngClass]="{ 'btn-sm' : window.screen.width < '575.5px' }"> ...

Is there a way to retrieve the XML data transmitted via WCF Service when using `Add Service Reference`?

If I have the following code in my application: var service = new Namespace.ServiceClient(); var req = new Namespace.Request(); req.Property1 = "Value1"; req.Property2 = 4.0; var res = service.Call(req); DisplayText(res.Result.ToString()); service.Close() ...

Guide to accessing and modifying attributes of Ionic components in TypeScript file

I am curious about how to manipulate the properties or attributes of an Ionic component from a TypeScript file. For example, if I have an input component on my HTML page: <ion-item> <ion-input type="text" [(ngModel)]="testText"></ion ...

Having difficulty pushing code from the Vs Code interface to gitlab. Receiving error message "Git: [email protected]: Permission denied (publickey, keyboard-interactive)"

Being a Mac user, I've encountered an issue where my VS Code connection to GitLab seems incomplete. While I am able to commit code using the VS Code interface, I struggle with pushing the code to the repository directly from VS Code. Instead, I resort ...

Using an aria-label attribute on an <option> tag within a dropdown menu may result in a DAP violation

Currently, I am conducting accessibility testing for an Angular project at my workplace. Our team relies on the JAWS screen reader and a helpful plugin that detects UI issues and highlights them as violations. Unfortunately, I've come across an issue ...

Closing a popover in NG-bootstrap from its container

I'm working on a container component named file-container, which includes an ngbPopover button. Inside the popover, there is another component used for selecting a file to upload. <button type="button" class="btn btn-secondary popover-btn ...

Dexie is alerting us to a problem with a call that occurs before initialization

When setting up my application, I encountered an error related to the Courses Entity Class being called before initialization in my Dexie Database. Despite checking my code, I couldn't find any issues and there was no documentation available for this ...

Why is it that my service in the Angular project doesn't fetch data after I make changes and reload the page?

When a user selects a customer, I have a method that fetches the customer's ID from the database and saves it to local storage. However, if I make changes to my code and refresh the page after selection, it doesn't fetch the customer ID. How can ...