I need a simply code that need to check the URL is 404, 500 or null (invalid) to just let the good ones pass to an Iframe in Angular v4.3.0. Something like this:
Component View:
<iframe [src]="goodUrl"></iframe>
Component .ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component ({
selector: 'iframe-component',
templateUrl: './iframe.template.html'
})
export class IframeComponent implements OnInit {
goodUrl: string = "http://www.idonotexist.com";
constructor( private HTTP: HttpClient ) {}
ngOnInit () {
this.searchUrl( this.goodUrl );
}
searchUrl( url ) {
this.HTTP.get( url ).subscribe(
(next: any) => { console.log('let the good URLs pass') },
(error: any) => { console.log('stop the bad ones and tell me what they are? 404, 500 or null') },
() => { console.log( 'the loop is complete' ) }
);
}
}
The problem with my tests it is that even the good Url's it's not passing in the test and just some few pass.
The reason that I need to test these URL's it's just to let the REAL URL's to pass to the Iframe and show them, but if in the test the URL is broken or the origin website is dead I want to do not show them in the Iframe and send a request back to the server inform that broken URL (website) it's not allowed in the Iframe component.
Best regards.