分享web开发知识

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

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

简单的ajax请求处理 | Django

发布时间:2023-09-06 01:30责任编辑:胡小海关键词:暂无标签
from django.shortcuts import renderfrom django.http import JsonResponse# 针对ajax设置csrf_tokenfrom django.views.decorators.csrf import csrf_protect, csrf_exemptdef index(request): ???return render(request, "index.html")@csrf_exemptdef handle_ajax(request): ???if request.method == ‘POST‘: ???????user_name = request.POST.get("username", "") ???????pass_word = request.POST.get("password", "") ???????print(user_name, pass_word) ???????if user_name == pass_word: ???????????return JsonResponse({"ret":1}) ???????else: ???????????return JsonResponse({"ret":2})
<head> ???<meta charset="UTF-8"> ???<title>Title</title> ???<script src="{% static ‘js/jquery-3.2.1.js‘ %}"></script></head><body> ???<h1>ajax的发送异步请求</h1> ???<button class="send_Ajax">Post发送请求</button> ???<!-- jquery实现ajax发送post请求--> ???<script> ???????// 选取标签,执行点击事件 ???????$(".send_Ajax").click(function(){ ???????????$.ajax({ ???????????????url:"/handle/", ???????????????type:"POST", ???????????????data:{username:123,password:123}, ???????????????success:function(data){ ???????????????????if(data.ret==1){ ???????????????????????alert("用户名等于密码!") ???????????????????}else{ ???????????????????????alert("用户名不等于密码!") ???????????????????} ???????????????} ???????????}); ???????}) ???</script></body></html>

简单的ajax请求处理 | Django

原文地址:http://www.cnblogs.com/pymkl/p/8046523.html

知识推荐

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