Sample Ajax GET/POST Request in Django

Let us make a test scenario here:
A dropdown field which on change we are going to send a Get/Post request to django and return response.

Let us start coding....

<select id="select_dropdown">
<option value='joshua'>joshua</option>
<option value='peter'>peter</option>

Lets make a Ajax request after change in dropdown field.


var e = document.getElementById("select_dropdown");
var value = e.options[e.selectedIndex].value;

    url: "your-url",
    type: "post", // or "get"
    data: value,
    success: function(data) {



Here on change of an post request is called. Now lets handle the view.

import json
def post(request):
if request.POST(): #os request.GET()
    get_value= request.body
    # Do your logic here coz you got data in `get_value`
    data = {}
    data['result'] = 'you made a request'
    return HttpResponse(json.dumps(data), content_type="application/json")

So you will get a pop-up message like:

Thats all.

comments powered by Disqus