一、url
from django.contrib import admin
from django.urls import path
from appo1 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('ajax_submit/',views.ajax_submit),
]
二、views
# Ajax POST 提交数据
def ajax_submit(request):
print(request.POST) #客户端发来的数据
return render(request,'ajax_submit.html')
三、html+ajax
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p align="center">用户名:<input id="vname" name="userName" type="text" placeholder="用户名" AUTOCOMPLETE="off"></p>
<p align="center">密 码:<input id="vpwd" name="password" type="password" placeholder="密码" AUTOCOMPLETE="off"></p>
<p align="center"><input type="button" onclick="AjaxSubmit();" value="提交"></p>
<script src="/static/jquery-1.12.4.min.js"></script>
<script>
function AjaxSubmit(){
var name=$("#vname").val()
var password=$("#vpwd").val()
alert(name)
$.ajax({
url:'/ajax_submit/',
type:'POST',
data:{vname:name,vpwd:password},
success: function (arg) {
}
});
}
</script>
</body>
</html>