栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python > python框架

Django之ajax相关介绍

python框架 更新时间:发布时间: 百科书网 趣学号

AJAX

Asynchronous Javascript And XML是 "异步Javascript和XML"。即使用 Javascript 语言与服务器进行异步交互,传输的数据为XML。

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

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

优点:

AJAX使用Javascript技术向服务器发送异步请求;

AJAX无须刷新整个页面;

因为服务器响应内容不再是整个页面,而是页面中的局部,所以AJAX性能高;

缺点:

AJAX并不适合所有场景,很多时候还是要使用同步交互;

AJAX虽然提高了用户体验,但无形中向服务器发送的请求次数增多了,导致服务器压力增大;

因为AJAX是在浏览器中使用Javascript技术完成的,所以还需要处理浏览器兼容性问题;

大概步骤:

step 1: var xmlhttp=XMLHttperquest()

step 2: xmlhttp.open("")

step 3: xmlhttp.send("name=klvchen") # 请求体的内容如果为 GET 请求则为 send(null)

step 4: if(xmlhttp.readyState===4 && xmlhttp.status===200) # 监听

相关推荐:《Python视频教程》

ajax 发送GET请求

创建一个 Ajax_lesson 项目 和 app01 应用

修改 urls.py 文件

from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index),
    path('ajax_receive/', views.ajax_receive),
]

在 tempates 文件夹中添加 index.html 文件




    
    Title


ajax提交


在 views.py 上修改

from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
    return render(request,'index.html')
def ajax_receive(request):
    print('ok')
    return HttpResponse("hello world2")

ajax 发送POST请求

修改 index.html 文件




    
    Title


ajax提交


修改 views.py 文件

from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
    return render(request,'index.html')
def ajax_receive(request):
    if request.method=="POST":
        print("req.POST", request.POST)
    return HttpResponse("hello world2")

在 settings.py 文件中注释

#'django.middleware.csrf.CsrfViewMiddleware',  

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/13052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号