Error at the return of an AJAX POST in DJANGO

69 views
Skip to first unread message

Walter Randazzo

unread,
Oct 25, 2020, 5:05:28 PM10/25/20
to Django users
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

rbar...@xcaretresearch.com

unread,
Oct 25, 2020, 5:42:40 PM10/25/20
to Django users
Not sure, but looks like an extra closing bracket in line 9 of the ajax code

Malcolm MacKinnon

unread,
Oct 25, 2020, 5:48:06 PM10/25/20
to django...@googlegroups.com
I'm not a JS expert, but is your success function defined properly?
if(e="OK") vs. if(e=="OK")
> --
> 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...@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.

Walter Randazzo

unread,
Oct 25, 2020, 6:06:49 PM10/25/20
to django...@googlegroups.com
HI Macolm,

I am checking out  the response value in the  and it shows OK in the browser dev tool . So i guess the function is not defined properly....

image.png

Walter Randazzo

unread,
Oct 25, 2020, 6:10:31 PM10/25/20
to django...@googlegroups.com
hi there,

As far as I check its OK.

Thanks for replay.



--
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...@googlegroups.com.

Trevor Woolley

unread,
Oct 25, 2020, 6:19:29 PM10/25/20
to django...@googlegroups.com

Walter,

 

If your copy and paste is accurate you have too many closing brackets in the ajax section.

 

Try removing one of these from the end of the ajax section:

    });

 

 

From: django...@googlegroups.com <django...@googlegroups.com> On Behalf Of Walter Randazzo
Sent: Monday, 26 October 2020 9:09 AM
To: django...@googlegroups.com
Subject: Re: Error at the return of an AJAX POST in DJANGO

 

[External email] Please be cautious when clicking on any links or attachments.
---

Walter Randazzo

unread,
Oct 25, 2020, 6:28:45 PM10/25/20
to Django users
I tried with no luck. When i remove it  it brings an error.

Malcolm MacKinnon

unread,
Oct 25, 2020, 8:06:00 PM10/25/20
to django...@googlegroups.com
In your success function, you are assigning the variable e = 'OK', so in your if statement, it appears to throw an error. If you change it to  if(e=="OK"), does it work? Note the two equal signs, == .

leon.vaks

unread,
Oct 25, 2020, 8:33:38 PM10/25/20
to django...@googlegroups.com
  t.f  



Sent from my Samsung Galaxy smartphone.


-------- Original message --------
From: Walter Randazzo <wwran...@gmail.com>
Date: 10/25/20 6:10 PM (GMT-05:00)
Subject: Re: Error at the return of an AJAX POST in DJANGO

leon.vaks

unread,
Oct 25, 2020, 8:33:38 PM10/25/20
to django...@googlegroups.com




Sent from my Samsung Galaxy smartphone.


-------- Original message --------
From: Walter Randazzo <wwran...@gmail.com>
Date: 10/25/20 6:10 PM (GMT-05:00)
Subject: Re: Error at the return of an AJAX POST in DJANGO

Walter Randazzo

unread,
Oct 25, 2020, 8:48:15 PM10/25/20
to Django users
HI Mac,
I've changed the if statement  to if(e=="OK")  but  the message error persist.
Note: I have explore the value of the return in the webbrowser dev tools and it contains "OK"


Nikitha Bangalore Escorts

unread,
Oct 26, 2020, 5:55:54 AM10/26/20
to Django users
hmm
Reply all
Reply to author
Forward
0 new messages