I have a very basic Django view that, once it receives a request, should send a response to the page that the user is visiting, so that i can display a message on that page, without having to refresh the page.
Here is my view:
def Action(request):
response = ''
if request.method == 'POST':
if request.POST.get('ajaxform', False) == 'submit':
data = request.POST.get('formdata', False)
mymodel = Userdata()
mymodel.save()
response = 'Submitted!'
return HttpResponse(json.dumps(response), content_type='application/json')
Basically, when a POST request is received, this view should send a JSON response to the template with the message Submitted
.
This view works, when i'm browsing my template, on my Dev tools i can see the JSON response Submitted
being received by the page, but i don't understand how can i show it using Jquery.
Can someone point me out on how could i create a Jquery function that is triggered only when a Json response is received by a Django view?
Basically, in this case, when the Django view fires the response Submitted
, my Jquery function should receive that message and do some stuff with it.
I tried this:
$.ajax({
url: "/mytemplate/Action",
type: 'get',
dataType: 'json',
success: function (data) {
console.log(data)
}
});
The problem with this function, though, is that nothing is being printed in my console, although i can see the response on my Network tab.