Sunday, October 25, 2020

Re: Error at the return of an AJAX POST in DJANGO

I'm not a JS expert, but is your success function defined properly?
if(e="OK") vs. if(e=="OK")

On Sun, Oct 25, 2020 at 2:06 PM Walter Randazzo <wwrandazzo@gmail.com> wrote:
>
> Hi buddies, I have a POST request from ajax. At the backend some records are updated in the django view, this is done fine but an error comes up and the page isnt reloaded.
>
> **This is the error:**
>
> > SyntaxError: Unexpected token O in JSON at position 0
>
> **This is the ajax:**
>
> $.ajax({
> headers: { "X-CSRFToken": token },
> "url": '/articulos/massup/', "type": "POST",
> "dataType": "json", data: data,
> success: function(e){
> if(e="OK"){
> location.reload(true);
> }
> },
> error: function(a,b,c){
> alert(c);
> }
> });
> });
>
> **Thi is the view:**
>
> @csrf_exempt
> def massup(request):
> template_name = "articulos/articulos_ok.html"
> contexto={}
> if request.method=="GET":
> cat = Articulos.objects.all().order_by("codigo")
> contexto={"obj":cat}
> if request.method=="POST":
> codigos=request.POST.getlist("codigos[]")
> porcentaje = codigos[0]#el primer elemento de la lista es el porcentaje
> porcentaje=Decimal(porcentaje)
> codigos= [int(x) for x in codigos]#Convierte la lista en integer
> art_change = Articulos.objects.filter(pk__in=codigos)
> i=0
> for item in art_change:
> if i!=0: #Excluye el primer item ( el porcentaje)
> precioant=item.precio
> precionuevo=(precioant + (porcentaje * precioant/100))
> item.precio=precionuevo
> item.save()
> i=i+1
> return HttpResponse("OK")
> return render(request
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/124085ff-0700-4361-9377-02596b161c4fn%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK8W3Xrftk-3SeGp-EmJVtPzhvWYQp-ZigkHouLOEpDd1W%2B8FA%40mail.gmail.com.

No comments:

Post a Comment