AJAX即「Asynchronous JavaScript and XML」(異步的JavaScript與XML技術),指的是一套綜合了多項技術的瀏覽器端網頁開發技術。Ajax的概念由傑西·詹姆士·賈瑞特所提出。css
傳統的Web應用容許用戶端填寫表單(form),當提交表單時就向網頁服務器發送一個請求。服務器接收並處理傳來的表單,而後送回一個新的網頁,但這個作法浪費了許多帶寬,由於在先後兩個頁面中的大部分HTML碼每每是相同的。因爲每次應用的溝通都須要向服務器發送請求,應用的迴應時間依賴於服務器的迴應時間。這致使了用戶界面的迴應比本機應用慢得多。html
與此不一樣,AJAX應用能夠僅向服務器發送並取回必須的數據,並在客戶端採用JavaScript處理來自服務器的迴應。由於在服務器和瀏覽器之間交換的數據大量減小,服務器迴應更快了。同時,不少的處理工做能夠在發出請求的客戶端機器上完成,所以Web服務器的負荷也減小了。jquery
path('ajax_test/', views.ajax_test),ajax
def ajax_test(request): if request.method == "GET": return render(request, 'ajax_test.html') else: username = request.POST.get("username") password = request.POST.get("password") print(username, password) data = username + " " + password return HttpResponse(data)
<head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> </head> <body> {% csrf_token %} <p> <input type="text" class="user" placeholder="用戶名"> </p> <p> <input type="password" class="pwd" placeholder="密碼"> </p> <button id="ajax_test">Ajax提交</button> <script> $("#ajax_test").click(function () { let user = $(".user").val(); let pwd = $(".pwd").val(); $.ajax({ url: "/ajax_test/", type: "POST", data: { username: user, password: pwd, csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val() }, success: function (data) { alert(data) } }) }) </script>
path('up_json/', views.up_json),json
def up_json(request): import json data = json.loads(request.body.decode("utf-8")) print(data, type(data)) return HttpResponse("提交成功")
<div style="margin-left: 100px;margin-top: 50px;"> <button class="up_btn">上傳json數據</button> </div> <script> $(".up_btn").click(function () { $.ajax({ url: "/up_json/", type: "POST", contentType: "json", header: {"X-CSRFToken": $("[name='csrfmiddlewaretoken']").val()}, data: JSON.stringify({ k1:"abc", k2:"xyz", k3: "v3", }), success: function (data) { console.log(data); alert(data); } }) }) </script>
點擊「上傳json數據」後端
path('up_file/', views.up_file),瀏覽器
def up_file(request): import os if request.method == "POST": file_obj = request.FILES.get("file_name") name = file_obj.name with open(os.path.join("media", name), 'wb') as f: for block in file_obj: f.write(block) return HttpResponse("上傳完成!")
<form action="/up_file/" method="post" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="file_name"> <input type="submit"> </form>
選擇文件 1.jpg,並點擊提交服務器
path('ajax_up_file/', views.ajax_up_file),app
def ajax_up_file(request): import os if request.method == "POST": file_obj = request.FILES.get("file_name") name = file_obj.name with open(os.path.join("media", name), 'wb') as f: for block in file_obj: f.write(block) return HttpResponse("上傳完成!")
<form action="/ajax_up_file/" enctype="multipart/form-data"> {% csrf_token %} <input type="file" class="file_name"> {# 不能用submit屬性#} <input type="button" class="ajax_up_file" value="ajax上傳文件"> </form> <script> $(".ajax_up_file").click(function () { let formdata = new FormData(); formdata.append("csrfmiddlewaretoken", $("[name='csrfmiddlewaretoken']").val()); formdata.append('file_name', $(".file_name")[0].files[0]); $.ajax({ url: "/ajax_up_file/", type: "POST", processData: false, contentType: false, data: formdata, success: function (data) { console.log(data); } }) }) </script>
選擇文件 2.jpg異步
點擊ajax上傳文件
基本操做到此結束!