ajax--->简单加法小练习
index.HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
</head>
<body> <input type="number" class="a"> + <input type="number" class="b"> = <input type="number" class="c">
<p><button class="Ajax_send">Ajax_send</button></p> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <script>
$(".Ajax_send").click(function () {
$.ajax({
url:"/get_ajax/",
type:"GET",
data:{
a:$(".a").val(),
b:$(".b").val()
},
success:function (data) {
$(".c").val(data)
}
})
})
</script> </body>
</html>
views.py
from django.shortcuts import render,HttpResponse
# Create your views here. def index(request):
return render(request,"index.html") def get_ajax(request):
first=request.GET.get("a")
second=request.GET.get("b")
reg= float(first)+float(second)
return HttpResponse(reg)
urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',views.index),
url(r'^get_ajax/', views.get_ajax),
]