Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!