1

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,template_name,contexto)

1 Answer 1

1

The datatype of your Ajax call is defined as JSON, but you are not returning JSON data to the Ajax function. Your specific error occurs when the data returned from the call is not in the expected JSON format. The POST section of your view returns an HttpResponse, not JSON data.

So simply import JsonResponse and use it instead of HttpResponse.

In your view:

from django.http import JsonResponse

 """
 your view code here, then end your POST section with:
 """

        data['success'] = True
        return JsonResponse(data)

then update your Ajax function to read the response appropriately. I changed your variable 'e' to 'data' with key 'success' simply for greater clarity about what is going on. Remember, JSON is a lot like a python dictionary:

success: function(data){
if(data['success'])...
Sign up to request clarification or add additional context in comments.

3 Comments

It worked with your instructions. The problem was how the response was set as you said. Thanks for share your knowledge.
@wwrandazoo Glad to hear it. Since it answered your problem, please consider marking the answer as the working solution (the green check mark to the left of the response) so I get credit for it. Thank you.
I did it several times, but a message pop up saying "you don have enough reputation to do that". Sorry but thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.