Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
How to put the response of a post-request into a new window/tab

I have this working example of how to use POST and put the server-generated html in a new window:

const response = await this.axiosInstance.post(url, postData)
const htmlResponse = response.data
const newWindow = window.open()
newWindow.document.write(htmlResponse)

However I'm not sure if this is the best or even correct way to do it. I see a few warnings with this. I get a warning on the last line that newWindow is possibly 'null'. If switching to TypeScript I am also not sure the type of htmlResponse. I would assume Document, but write() only supports string, so it might be the correct classification.

Is there a better way to put the content of a post-request into a new window?

Update: The content-type is of type text/html; charset=UTF-8, not sure if it changes anything.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

What you have there might be enough for your use case. One might recommend waiting for some form of a ready event before manipulating the DOM.

const newWindow = window.open();
newWindow.onload = (event, f, g, h) => newWindow.document.write('Do great things.');

A much better solution might be fetching actual data instead of an HTML string from the server, creating a template page to receive the data, and rendering a well-formatted page.

The Window.postMessage() API could be used to send data between parent and child windows. This is especially useful if you need to circumvent the Same-origin policy.

I have made several assumptions, and this suggestion might not work for your use case.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda