Working with <html> response data in place of Json in Angular 6

My Angular 6 application needed to call a web service that returns HTML data, which I then had to display within a div.

Below is an example of the HTML response data from the service:

<html>
 <head>
<title>Chart : 180: Abraham, Male, 1 Year(S)</title>
</head>
<frameset border=0 rows="145,*">
  <frame noresize id="t_pan" name="t_pan" src="id=180" scrolling="no">
  <frameset border=0 cols="175,*">
    <frame noresize id="l_pan" name="l_pan" src="id=180">
    <frame noresize id="r_pan" name="r_pan" src="Pt&id=1937181">
  </frameset>
</frameset>
</html>

This is how I called the API that returned the above HTML data:

getDetailData(url:string): Observable<any> {
    return this.http.get(url, {headers :{'Accept': 'text/html', 'responseType': 'text' as 'json'}})
        .map(data => {
            data;            
    });

In my component, I invoked the service like this:

const url = "http://10.98.12.1:2009/PChart?id=180";
this.personService.getDetailData(url).subscribe(res =>{
     console.log(res);
});

The issue:

"error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHtt…, text: "..."

Response :

https://i.sstatic.net/43rLk.png

My Solution :

I realized my mistake was using the wrong path. I solved it by using an iframe to load and display the data correctly.

Answer №1

Adjust the responseType to:

'responseType': 'text';

When making the API call, use the following:

const apiUrl = "http://10.98.12.1:2009/PChart?id=180"; //currently hardcoded
this.personService.getDetailData(apiUrl).subscribe(response => {
    this.name = response;
});

Here is the HTML snippet:

<div [innerHTML]="name"></div>

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

Getting the FormArray value in an Angular TypeScript file

Having trouble accessing the form array value in my TypeScript file - it's coming up as a blank array. Here's my HTML code: <mat-form-field class="example-full-width" > <mat-label>Locations </mat-lab ...

execute a JAR file on a Windows Server

My application is running on a Windows Server using Spring Boot and Angular 2. I am looking for the best way to run the executable jar file in this environment. I have some questions regarding this setup: What is the recommended approach for running the ...

The sunset of Cordova UIWebView support for iOS platforms

As I work on developing an Application with Ionic/Cordova, I've come across some concerning rumors about the deprecation of Cordova UIWebView on iOS to take effect in April 2020 (which would mean it is already deprecated). This has raised questions fo ...

Ionic 4 - Best practices for handling asynchronous data with multiple observables

I successfully accessed this route: http://localhost:8100/questions/question?id=3 However, I am facing a challenge in handling two subscribers simultaneously. The first subscriber is responsible for loading the questions array from an external service. ...

The node package for the 'browser' builder '@angular-devkit/build-angular' could not be located

My attempt at deploying my application to Heroku has hit a roadblock. While Heroku local web functions perfectly, I've encountered issues when trying to include 'npm i @angular-devkit/build-angular'. Despite scouring the internet for solutio ...

Having trouble utilizing a JavaScript file within TypeScript

I am currently exploring the integration of Three.js into an Angular application. Following the documentation, I imported Three.js using the following line: import * as THREE from 'three'; In addition, I installed the types for Three.js with th ...

A guide to sorting through in-app notifications in REACT-NATIVE based on their read status

Incorporating two headings, "Unread" and "Read", into the notification system is my goal. When opened, the Unread Notifications should be displayed beneath the Read notifications. This data is being retrieved from an API. Each notification contains a key ...

Angular 7's Singleton Service Feature

My service setup looks like this: export abstract class ILoggingService { // abstract functions here } export class LoggingService implements ILoggingService { // service implementation } export class MockLoggingService implements ILoggingServic ...

Utilizing Jest and nest.js for testing with absolute paths

Looking at my jest configuration inside the package.json: "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "moduleDirectories":["node_modules", "src" ...

Develop a set of data series for HighCharts using JSON input

My task is to aggregate data for a HighCharts Column Chart based on the JSON provided below. The Y-Axis data in HighCharts should have 0 when there is no corresponding data for the X Series. Here's the initial JSON: { "groups": { "1567310 ...

Guide to integrating global interfaces into your Nuxt project

Recently diving into the world of Nuxt 3, I've encountered a challenge while exploring TypeScript functionalities. My current goal is to create a versatile NavBar featuring multiple buttons with unique links. To achieve this, I aimed to establish an ...

Is it possible to create a return type structure in TypeScript that is determined by the function's argument?

I'm currently stuck on developing a function that takes a string as an argument and outputs an object with this string as a key. For example (using pseudo code): test('bar') => {bar: ...} I am facing difficulties in ensuring the correct ...

Resolve the result of the HttpComponent within the Service component

Consider this example involving HttpClient: In Service configData: string[]; fetchData(): Observable<string[]> { return this.http.get<string[]>('./assets/config.json'); } getConfigValue(key: string): string { if ...

The compiler is unable to locate the node_module (Error: "Module name not found")

Error: src/app/app.component.ts:4:12 - error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try npm i @types/node and then add node to the types field in your tsconfig. 4 moduleId: module.id, When att ...

The bundling of Angular 2 and RxJS is not happening in Typescript

This is my tsconfig.json { "compilerOptions": { "target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "n ...

The blank screen mystery: ionic and Google maps integration not playing nice

I've been struggling to integrate Google Maps into my web application. Unfortunately, all I see is a blank screen with no errors. Here's the current code snippet that I have. It seems like there might be an issue with the mapElement variable, but ...

Detecting Changes and Handling Events Beyond Angular's Scope

After setting up my Angular 7 Application, I integrated a large external library successfully. The only issue I encountered was with an event from this external source. An event listener in the external source triggers and sends data to my Angular App. e ...

Tips for automatically scrolling mat-select option to the top when mat-select is closed

Is there a way to automatically scroll the mat-select options to the top after closing it? I am faced with a situation where I have numerous options in my mat-select dropdown. If I select an option from the bottom and then close the mat-select, is there a ...

Issue: Unable to find the last element using the function findLast in Jest test

While working on a test using jest in Angular, I encountered the following error: Error: TypeError: columnDefs.findLast is not a function The method I am attempting to test is as follows: onKeyDown(key: any) { const columnDefs = this.params.columnApi ...

NPM is currently experiencing difficulties installing or uninstalling packages

I'm having trouble installing or uninstalling any npm package because of an error that says "Cannot find module 'emoji-regex'." Does anyone know what might be causing this issue? Here are my configurations. I also tried running "npm install ...