1. Intro

Ajax can send request without refreshing the webpage.

1.1 Get

function clickMe(){
    $.ajax({
        url: "/user/list",
        type: "get",
        data: {
            name: $("#input1").val(),
            passwd: $("#input2").val()
        },
        // data: $("#form1").serialize(),
        dataType: "JSON",
        success:function(res){
            console.log(res.data);
        }
    })
}
...
<div>
    <input type="button" value="button" onclick="clickMe();" />
</div>
...

Or, we can use JQuery to bind the event of button click.

1.2 Post

# views.py
from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def ajax_res(request):  # post request
    data_dict = {"status": True, "data": [1, 2, 3]}
    json_string = json.dumps(data_dict)
    return HttpResponse(json_string)
    # return JsonResponse(data_dict)