Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return plain JSON when calling specific router URL

I have an Angular application that shows different pages. I can navigate by (hamburger) menu or by calling the specific route (e.g. http://localhost/samplepage). Now I want to return plain JSON content when entering a specific route (e.g. http://localhost/myjson).

How can I manipulate the response so that it throws away all the Angular generated component code and instead return my plain JSON?

like image 922
Torsten Weggen Avatar asked Sep 13 '25 00:09

Torsten Weggen


1 Answers

This seems to work:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  name = 'Angular';

  ngOnInit() {
    const obj = {
      name: 'Torsten',
      age: 54
    }
    const thefile = new Blob([JSON.stringify(obj)], { type: "application/json" }) ;
    let url = window.URL.createObjectURL(thefile);
    window.location.href = url;
  }
}
like image 88
Torsten Weggen Avatar answered Sep 15 '25 16:09

Torsten Weggen