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

128
Views
¿Cómo garantizar que la solicitud GET de recuperación se ejecute después de que se actualice la base de datos?

Tengo problemas para garantizar que mi solicitud GET en Javascript se ejecute después de PUT.

A veces, la promesa JSON regresa con datos actualizados, ¡a veces no!

Parece ser aleatorio. Siempre se muestra correctamente al recargar la página.

Recibo el error "Fetch fail loading: PUT" en mi consola a pesar de que la base de datos se actualizó correctamente.

Sé de await/async pero no estaba claro si esta era mi solución y, de ser así, cómo implementaría con la sintaxis correcta.

Aquí está mi JS:

 function like(id) { like_button = document.getElementById(`like-button-${id}`); if (like_button.style.backgroundColor == 'white') { fetch(`/like/${id}`, { method:'PUT', body: JSON.stringify({ like: true }) }); like_button.style.backgroundColor = 'red'; } else { fetch(`/like/${id}`, { method:'PUT', body: JSON.stringify({ like: false }) }); like_button.style.backgroundColor = 'white'; } fetch(`/like/${id}`) .then(response => response.json()) .then(post => { like_button.innerHTML = post.likes; }); }

En caso de que sea útil, aquí está mi vista en views.py:

 @csrf_exempt def like(request, id): post = Post.objects.get(id=id) user = User.objects.get(username=request.user.username) if request.method == "GET": return JsonResponse(post.serialize(), safe=False) if request.method == "PUT": data = json.loads(request.body) print(data.get("like")) if data.get("like"): post.Likes.add(user) else: post.Likes.remove(user) post.save() return HttpResponse(status=204)

Mensaje de error de la consola

about 4 years ago · Juan Pablo Isaza
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!