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

251
Visualizações
Refreshing table after performing ajax POST request

I am doing a POST request from ajax to python flask like below.

function humval(val1,val2){
    var dict = {"url":$('#url'+val1).val(),"status":val2};
            $.ajax({
            url: '/testroutez',
            contentType: 'application/json;charset=UTF-8',
            data : JSON.stringify(dict),
            type: 'POST',
            success: function(response){
                alert('success');
                console.log(response);
            },
            error: function(error){
                alert('error occured');
                console.log(error);
            }
        });
}

Html code is handling jinja format

<tbody>
    {% for value in myData %} 
    <tr>
        <td class="urlTd">
            <input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
            {{ value["url"] }}
        </td>
        <td>{{ value["org"] }}</td>
        <td>{{ value["val_date"] }}</td>
        <td>
            <input type="hidden" value="{{value['url']}}" id='url{{ loop.index }}'>
            <input type="button" onclick="humval('{{ loop.index }}','valid')" type="button" class="far fa-check-circle hButton" id="hvalid">
            <input type="button" onclick="humval('{{ loop.index }}','invalid')" type="button" class="fas fa-times-circle hButton" id="hinvalid">
        </td>
    </tr>
    {% endfor %} 
</tbody>

flask code is

@data_sources_api.route('/testroutez', methods=["POST"])
def testroutez():
    if loggedin():
        if request.method== 'POST':
            print("request received")
            data_json = request.get_json()
            result_l = MlData.objects(hum_proc='null')
            myData = mldatas_schema.dump(result_l)
            return render_template('users/validation.html', myData = myData)

What is tried is to render the template again with new value myData. But as the request is passing from ajax, it wont work and again the whole page will get refreshed. But I want the table alone to refresh with new values. How it is possible to do like that

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Jinja does not support dynamic page content(this is because the template is rendered only once when the page is loaded), this means that if your data changes you have to take care of updating them. To do this you have two possibilities, the first is to refresh the page, the second is to request the updated data from the server, and via javascript update the contents of the table within your html page, removing all the records and populating it with the new data returned by the server.

Take a look at the following link: Display data streamed from a Flask view as it updates

over 4 years ago · Santiago Trujillo Relatório

0

location.reload() will solve the refresh issue

So the ajax request will be like below

function humval(val1,val2){
    var dict = {"url":$('#url'+val1).val(),"status":val2};
            $.ajax({
            url: '/testroutez',
            contentType: 'application/json;charset=UTF-8',
            data : JSON.stringify(dict),
            type: 'POST',
            success: function(response){
                location.reload();  
                // alert('success');
                console.log(response);
            },
            error: function(error){
                alert('error occured');
                console.log(error);
            }
        });
}

I am not sure whether this is the right way. If there is anything better, i am waiting for those

over 4 years ago · Santiago Trujillo 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