Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
JS - function sends 2 POST requests

I have JS function that sends POST request

let username = document.querySelector('.fw-user').innerText;
    if (document.querySelector('.follow')) {
        let followButton = document.querySelector('.follow');

        followButton.addEventListener('click', () => {
            fetch(`/follow/${username}`, {
                method: 'POST',
            })
            .then(() => {
                window.location.reload();
            })
        })
    }

urls.py

path("follow/<creator>", views.follow, name="follow")

views.py

@csrf_exempt
@login_required
def follow(request, creator):
    if request.method == "POST":
        get_acc_id = User.objects.get(username=request.user.username)
        get_creator_name = User.objects.get(username=creator)
        follow = Followers.objects.create(follower_id=get_acc_id.id, creator=get_creator_name.username)
        get_creator_name.followers += 1
        get_creator_name.save()
        return HttpResponse(status=204)

html

<button type="button" class="follow btn btn-outline-light btn-sm">Follow</button>

When i press this button, function sends 2 POST requests, instead of 1.

Screenshots:

Network

Console

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem was not with the fetch, it was because follow function executes when DOM loaded, and I had another function with same event handler. Because of this, the event handler was executed several times.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda