分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

django的ajax提交示例

发布时间:2023-09-06 02:06责任编辑:赖小花关键词:django

两条路由:

path(‘ajax_submit/‘, views.ajax_submit),path(‘add/‘, views.add),

在模版文件夹里写出html,add.html

def add(request): ???return render(request, ‘add.html‘) ???def ajax_submit(request): ???print(request.method) ???u = request.GET.get(‘username‘, None) ???p = request.GET.get(‘password‘, None) ???if u and u == ‘Eric‘ and p and p == ‘123‘: ???????????return HttpResponse(‘OK‘) ???else: ???????return HttpResponse(‘用户名或密码错误‘)

jquery实现ajax提交:

<!doctype html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>Document</title></head><body> ???<div> ???????<form action=""> ???????????<input id="username" type="text" placeholder="用户名" name="username"> ???????????<input id="password" type="text" placeholder="密码" name="password"> ???????????<input type="button" value="ajax提交"> ???????</form> ???</div> ???<script src="/static/jquery-1.12.4.js"></script> ???<script> ???????$(function(){ ???????????$(‘:button‘).click(function(){ ???????????????$.ajax({ ???????????????????url: ‘/app01/ajax_submit/‘, ???????????????????type: ‘GET‘, ???????????????????data: { ???????????????????????‘username‘:$(‘#username‘).val(), ???????????????????????‘password‘:$(‘#password‘).val(), ???????????????????}, ???????????????????success: function(data){ ???????????????????????if(data==‘OK‘){ ???????????????????????????alert(‘验证成功‘); ???????????????????????}else{ ???????????????????????????alert(data) ???????????????????????} ???????????????????????????????????????????} ???????????????}) ???????????}) ???????}) ???????????</script></body></html>
View Code

如果用户名和密码是Eric 和 123就显示验证成功,否则返回错误信息。

jquery的ajax的方式有$.ajax $.get $.post $.getJson都是ajax请求方式,本质上都是$.ajax

$.get(url="", data={})

django的ajax提交示例

原文地址:https://www.cnblogs.com/ericbai/p/9388781.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved