Check the return of window.open if it is JSON or PDF

0

Hello! I am creating an application using Angular, and request the data from an API using Observables with Angular 4. This Observable returns me a HASH and I use this hash to complete a route (a URL) to another API to download a PDF file . But this last API, it returns the pdf print ticket already downloaded on the client PC or a JSON file stating the error. The question is how can I or what can I use from Angular to handle the case where the JSON with error is returned? For when there is success, the ticket is downloading, but when there is error a new tab is presented in the browser showing JSON. I wanted a way to know if there was this error and display something on the screen. I'm using window.open to open this API URL. Here is the code for this particular function:

In the code below, I retrieve HASH to complete my URL to download the file, if I successfully complete it, put the data in a response.

ngOnInit() {
    let response = this.getBoletoService.getHashBoleto(this.profissional).subscribe(
      (data:any) => {
        if(data['sucesso'] == false){
          return 'Boleto será enviado por e-mail'
        } else {
          this.resposta = data
        }
        err => {
          this.router.navigate(['erro'])
        }
      }
    )
    if(response){
      this.dataloaded = true;

   }
  }

In the code below, when submitting a form, I check if there was a HASH response, I make a POST in the form data and use window.open ('$ {this.url} / $ {this.resposta [' hash ' I have a JSON application that does not have a JSON application. I have a JSON application that does not have a JSON application, but I can not handle it :(

OnSubmit(f){
    this.profissional.endereco = f.value
    if(this.resposta){
      this.profissional['boletoGerado'] = true
      this.atualizaEndereco.atualizaEndereco(this.profissional).subscribe(
        data => {
          this.router.navigate(['sucesso'])
          window.open('${this.url}/${this.resposta['hash']}')
        },
        error => {
          window.open('${this.url}/${this.resposta['hash']}')
          this.novoEndereco = false
        }
      );

Can anyone suggest me how to solve this or a better deal on the Front End side?

    
asked by anonymous 18.07.2018 / 16:34

0 answers