AJAX and Django, AJAX success not working
In an HTML page I have a table where one can drop items into and that is posted to the server via AJAX as follows:
function drop(ev, el) {
if (ev.target.tagName=="IMG") { return; }
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
el.appendChild(document.getElementById(data));
$.ajax({
type: 'POST',
url: server_url + currentPath,
data: {
'version': data
},
success() {
console.log(data, "ranked ");
},
});
}
on the same page, users submit an answer using a button which is linked to a function that sends a POST request to the server:
function NextPage(){
answer = $("#justification").val();
if (answer.length < 10){
document.getElementById("warning1").innerHTML = "Please provide an answer";
}
else {
$.ajax({
type: "POST",
url: server_url + currentPath,
data: {'answer' : answer},
success: function () {
window.location.href = server_url+ nextPath;
}
});
}
}
I can read the data from the post request on the server side (i.e., version and answer), and it gets saved as well, but then I cannot move to the next page, and there are no error messages; I noticed that the answer is now passed as a parameter on the URL; not sure what that means. I guess it gets stuck because of an error. I checked the currentPath and NextPath and they are correct. I think the problem is with the ranking object, because when I removed it, everything worked perfectly fine. Can someone spot something I am missing that caused this issue? here is my view:
@csrf_exempt
@login_required
def task_view(request ,visualisation, task, progress):
participant = get_object_or_404(Participant, user=request.user)
question_list = Question.objects.filter(complexity = 'N')
question = question_list[0]
answer, created = Answer.objects.get_or_create(participant=participant, question=question)
ranked_visualisation = SVG.objects.filter(name=visualisation, task=task)
if request.method == 'POST':
participant_answer = request.POST.get('answer')
current_version = request.POST.get('version')
for i in ranked_visualisation:
ranking, created = Ranking.objects.get_or_create(participant=participant, svg=i)
if ranking.svg.version == current_version:
ranking.ranking = 10
else:
ranking.ranking = 20
ranking.save()
tasks_completed = participant.tasks_completed
if participant_answer and not participant_answer.isspace():
tasks_completed = tasks_completed +1
else:
tasks_completed = tasks_completed
participant.tasks_completed = tasks_completed
participant.save()
answer.answer = participant_answer
answer.save()
context = {'task':task,'question': question, 'answer': answer, 'progress': progress}
return render(request, 'study/A.html', context)
here are my models:
class SVG(models.Model):
name = models.CharField(max_length=200)
version = models.CharField(max_length=200, default='none')
task = models.CharField(max_length=200, default='none')
def __str__(self):
return self.name
class Ranking(models.Model):
participant = models.ForeignKey(Participant, on_delete=models.CASCADE)
ranking = models.IntegerField(default=0)
svg = models.ForeignKey(SVG, on_delete=models.CASCADE)
class Answer(models.Model):
participant = models.ForeignKey(Participant, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
answer = models.TextField(blank=True, null=True)