I have an Ajax request that sends JSON data to a Django view, where a success message is then returned and displayed to the user.
The problem I am facing is Ajax success is not being called. I get an alert from the error part of the Ajax request with the HTTP status code 200 ("Received JSON successfully") but its not appended to the results div.
My understanding is that, I am possibly not returning the data correctly in the Django function to be used as HTML, hence why it is calling Ajax error.
jQuery (Ajax):
json_query = JSON.stringify(form_data);
$.ajax({
url: "/stix_json/",
type: "POST",
dataType: "html",
contentType: "application/json",
data: {
client_response: json_query,
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function(result){
$('#results').html(result);
},
error : function(xhr,errmsg,err) {
alert(xhr.status + ": " + xhr.responseText);
}
});
Django Views.py
The url /stix_json/ calls the function in views.py process_json
def process_json(request):
if request.POST.has_key('client_response'):
x = request.POST['client_response']
# Do something with JSON here
success = "Received JSON successfully"
return HttpResponse(success)
else:
return render(request, "stix_form.html")
I would appreciate if someone was able to explain why this issue occurs.