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

108
Visualizações
How to update page after fetch POST request

I am creating a simple chat room and am expecting the page to update when I enter a message into a text box and press enter.

The room is receiving messages from a URL, but I have to reload the page to see my new message whenever I post it.

How do I display these messages without having to reload the page?

fetch GET:

fetch('https://curriculum-api.codesmith.io/messages', { 'method': 'GET' })
    .then(response => response.json())
    .then(data => addText(data))

fetch POST:

  textBox.addEventListener('keydown', (e) => {
    if (e.code === 'Enter') {
      const func = async function postReply() {
        const currentDate = new Date()
        const message = textBox.value;
        textBox.value = ''
        return await fetch('https://curriculum-api.codesmith.io/messages',
          {
            method: 'POST',
            body: JSON.stringify({
              'message': message,
              'created_at': currentDate.toUTCString(),
              'created_by': 'Nate & Dewey'
            })
          })
          .then(response => response.json())
          // .then(json => addText(json.message))
      }
      func()
      // $('#result').load('https://curriculum-api.codesmith.io/messages');
    }
  })

addText() function (used by GET)

  function addText(data) {
    let messageNum = 0
    let current = data[messageNum]
    while (current) {
      const text = document.createElement('div');
      border.appendChild(text);
      text.innerText = current.message + ' (' + current.created_by + ')'
      messageNum++
      current = data[messageNum]
    }
  }
about 4 years ago · Juan Pablo Isaza
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