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

144
Visualizações
how to push or appand response data into web page

I am posting a comment with Axios (without refreshing page) client side to server. the comment saves successfully and sends saved data as a response. I want that response data shown on a web page after saving. I am not understanding how to do that, support me. here is my code into

    <script>
        const form = document.querySelector('.formPost');
 const pushComment = document.querySelector('#pushComments').innerHTML
const formEvent = form.addEventListener('submit', event => {
    event.preventDefault();
    const postId = document.querySelector('#postId').value;
    const adminId = document.querySelector('#adminId').value;
    const comment = document.querySelector('#comment').value;
    const commentData = {postId, adminId,comment};
 
   async function doComment(form){
 await axios({
    url:"/comment",
    method:'post',
    data:commentData,
}).then((responseData)=>{
   
  var  value = responseData.data
   document.getElementById('pushComments').innerHTML = value
    console.log(responseData.data)
})
 }
 doComment()
 function nulldata(){  
    const comment = document.querySelector('#comment').value=''
 }
 nulldata()
});

here sever side data

route.post('/comment', async (req,res)=>{

const user = req.user
    const comments = {
        postId:req.body.postId,
        admin: req.body.adminId,
        comment:req.body.comment,
        name:req.user.name,
        email:req.user.username
    }
    console.log( comments)
    const comment = await Comment.create(comments)
res.json({comment})

})

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

0

You're receiving a json response containing a comment-document from the server side.

Assuming you want to append the comment field from the response and pushComments is a simple <ul> you could simply do:

 const {comment} = responseData.data;
 const existingCommentsList = document.getElementById('pushComments').innerHTML;
 document.getElementById('pushComments').innerHTML = `${existingCommentsList}<li>${comment.comment}</li>`;
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