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

203
Visualizações
AJAX-based refresh without data duplication

I am confused about AJAX requests. I am using the load() function to refresh specific elements on the page. Unwanted duplication does not occur when there is only one matched element on the page.

{% for example in examples %}
<div class="refresh" id="single{{ example.id }}">
    <div class="container">
        <p> Different content </p>
    </div>
</div>
{% endfor %}

Assuming the above example, I would like to refresh the contents of a container with different contents in multiple elements on the page. In this case, if the 'examples' elements are four, there will be 8 after the refresh. I've tried everything I can find, but so far the data is duplicated.

#1 UPDATE
The script is placed in the base template, wrapped in $(document).ready(function(). AJAX of type GET on success the function $( ".class-to-refresh" ).load(" .class-to-refresh") is called.

PS The server displays a redundant request, but it does not duplicate data when there is only one item with the class class-to-refresh on the page. Otherwise, elements duplicate or overlap.

#2 UPDATE
Below I provide a simplified code containing the troubles (online version).

<!DOCTYPE html>
<html lang="en">
<head>
<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous">
</script>
<script>
        $(document).ready(function(){
            setInterval(function() {
                $.ajax({
                    url: location.href,
                    type: 'GET',
                    success: function(data) {
                        $( ".content" ).load(" .content" );
                    }
                });
            }, 3000)
        });
</script>
</head>
<body>

<div class="content" id="content1">
    Content
</div>

<div class="content" id="content2">
    Different content
</div>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok. The issue here is that you are doing your AJAX request twice. First with $.ajax(..) and then with ().load().

You should only use one of the functions.

Maybe what you want to do should look like this:

<!DOCTYPE html>
<html lang="en">
<head>
<script
  src="https://code.jquery.com/jquery-3.3.1.js"
  integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
  crossorigin="anonymous">
</script>
<script>
        $(document).ready(function(){
            setInterval(function() {
                $( ".content" ).load("ajax/url/file.html .content" );
            }, 3000)
        });
</script>
</head>
<body>

<div class="content" id="content1">
    Content
</div>

<div class="content" id="content2">
    Different content
</div>

</body>
</html>

From the load documentation:

$( "#result" ).load( "ajax/test.html #container" );

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

Probably the code isnt perfect but you can read more about .load() here.

So in Django if you have this in your template:

{% for example in examples %}
<div class="refresh" id="single{{ example.id }}">
    <div class="container">
        <p> Different content </p>
    </div>
</div>
{% endfor %}

then it will load all the existing example in examples and if you incert them with .load() you will have duplicated content.

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