1.Ajax简介

AJAX(Asynchronous Javascript And XML)——“异步的JavaScript与XML”。

Ajax使用Javascript语言与服务器进行异步的交互,传输的数据为XML(时至今日,传输的数据更多为Json格式)。

同步交互与异步交互

同步交互:客户端发出一个请求后,需要等待服务器响应结束后,才能发出第二个请求;

异步交互:客户端发出一个请求后,无需等待服务器响应结束,就可以发出第二个请求。

Ajax的特点:

  1. 异步交互
  2. 浏览器页面局部刷新

Ajax的优点:

  1. Ajax使用Javascirpt向服务器发送异步请求
  2. Ajax无须刷新整个页面

2.基于jQuery的Ajax实现

2.1 最基础的Ajax代码实现

2.1.1模板

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
</head>
<body>
<div class="content"></div>
<button class="Ajax">Ajax</button>
<script>
$(".Ajax").click(function () {
$.ajax({
url: '/test/',
type: 'get',
success: function (data) {
$('.content').html(data)
}
}
)
})
</script>
</body>
</html>

2.1.2 视图

from django.shortcuts import render,HttpResponse

# Create your views here.
def index(request):
return render(request,'index.html') def test(request):
return HttpResponse('hello Ajax')

实现效果:

2.2基于Ajax的计算器

2.2.1模板

<input type="text" id="num1">+<input type="text" id="num2">=<input type="text" id="ret"><button class="cal">计算</button>

 $('.cal').click(function () {
$.ajax({
url:'/cal/',
type:'post',
data:{
'n1':$('#num1').val(),
'n2':$('#num2').val(),
},
success:function (data) {
console.log(data);
$('#ret').val(data);
}
})
})

2.2.2视图

def cal(request):
print(request.POST)
n1 = int(request.POST.get('n1'))
n2 = int(request.POST.get('n2'))
ret = n1+n2
return HttpResponse(ret)

实现效果:

2.3基于Ajax的登陆组件

2.3.1模板

<form >
用户名 <input type="text" id="user">
密码 <input type="password" id="pwd">
<input type="button" value="submit" class="login_btn"><span class="error"></span>
</form>
$('.login_btn').click(function () {
$.ajax({
url:"/login/",
type:'post',
data:{
'user':$('#user').val(),
'pwd':$('#pwd').val(),
},
success:function(data){
console.log(data);
console.log(typeof data);
var data=JSON.parse(data);
console.log(data);
console.log(typeof data);
if (data.user){
location.href='http://www.baidu.com'
}
else {
$(".error").html(data.msg).css({"color":'red',"margin-left":'10px'})
}
}
})
})

2.3.2视图

def login(request):
print(request.POST)
user=request.POST.get("user")
pwd=request.POST.get('pwd')
user= User.objects.filter(name=user,pwd=pwd).first()
res= {"user":None,"msg":None}
if user:
res["user"] = user.name
else:
res["msg"]= "username or password wrong!"
import json
return HttpResponse(json.dumps(res))

Django——Ajax的更多相关文章

  1. Django ajax MYSQL Highcharts<1>

    Another small project with django/Ajax/Mysql/Highcharts. 看下效果图  - delivery dashboard .嘿嘿 是不是还蛮好看的. 废 ...

  2. django ajax练习

    这几天遇到了django ajax请求出错的问题,总结一下 前端js:我这里创建的是一个字典格式的数据,前端js收到字典之后也是要用字典的形式去解包后台传送过来的数据,比如我下面的写法:data['s ...

  3. 关于Django Ajax CSRF 认证

    CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session riding,通常缩写为CSRF或者XSRF,是一种对网站的 ...

  4. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  5. python学习-- Django Ajax CSRF 认证

    使用 jQuery 的 ajax 或者 post 之前 加入这个 js 代码:http://www.ziqiangxuetang.com/media/django/csrf.js /*======== ...

  6. django ajax 及批量插入数据 分页器

    ``` Ajax 前端朝后端发送请求都有哪些方式 a标签href GET请求 浏览器输入url GET请求 form表单 GET/POST请求 Ajax GET/POST请求 前端朝后端发送数据的编码 ...

  7. django ajax提交form表单数据

    后台: from django.shortcuts import render from django.shortcuts import redirect from django.shortcuts ...

  8. Django ajax提交 登录

    一.url from django.contrib import adminfrom django.urls import pathfrom appo1 import views urlpattern ...

  9. django ajax报错解决:You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set.

    Django版本号:1.11.15 django中ajax请求报错:You called this URL via POST, but the URL doesn't end in a slash a ...

随机推荐

  1. JMeter 不同线程组间变量传递

    JMeter元件都是有作用域的,而变量大多使用正则表达式提取器,要想在不通过线程组件使用变量参数,则需要设置全部变量 JMeter函数助手就提供了一个函数用于设置全局变量属性,实现的功能类似于在用户自 ...

  2. Qt调用自己编译的libglog.a出现问题

    我确定依据正确导入库后,依旧出现未定义的引用. undefined reference to _imp___ZN6google17InitGoogleLoggingEPKc 尝试过重新编译,调整编译参 ...

  3. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. mybatis 模糊查询 like的三种方式

    第一种 public void selectBykeyWord(String keyword) { String id = "%" + keyword + "%" ...

  5. windows telnet 模拟 http请求

    1. 开启windows自带的telnet客户端(控制面板 --> 程序 --> 启用或关闭windows功能 --> ) 2. 打开cmd,使用Telnet客户端 3. 按ctrl ...

  6. MySQL查看SQL语句执行效率

    Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看 SQL 语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好 ...

  7. Mysql SQL注入漏洞

    学习网址:http://www.cnblogs.com/rush/archive/2011/12/31/2309203.html https://www.cnblogs.com/sdya/p/4568 ...

  8. 2019最新迅为-i.MX6Q开发板资料目录

            迅为IMX6开发板: Android4.4系统  Linux + Qt5.7系统  Ubuntu12.04系统 部分案例:HMI:3D打印机:医疗设备:工控机:触控一体机:车载终端 核 ...

  9. 快速安装Java环境

    1.部署jdk8 jdk包地址:https://pan.baidu.com/s/1QNGpapGuex00F6HQ5pynHgtar -xzf jdk-8u60-linux-x64.tar.gz #安 ...

  10. 五子棋.html

    二维数组的定义 , canvas对象的使用 二维数组:以下用new,其实简化 [] 即可, var tArray = new Array(); //先声明一维 for(var k=0;k<i;k ...