(,开发技术)

时间:2024-05-06 22:21:56 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

前台表单:

<div action="login_in" method="post"> {% csrf_token %} <input type="text" name="user" id="user"> <input type="text" name="pwd" id="pwd"> <input type="submit" id="btn" value="登录"></div><script> $(function () { $("#btn").click(function () { var data = JSON.stringify({"user":$("#user").val(),"pwd":$("#pwd").val()}) $.ajax({ url:"login_in", contentType:'application/json;charset=UTF-8', type:"POST", data:data, beforeSend: function(xhr, settings) { var token = $('input[name=csrfmiddlewaretoken]').val() xhr.setRequestHeader("X-CSRFToken",token); }, {% comment %}beforeSend:function (xhr,settings) { xhr.setRequestHeader("X-CSRFtoken",$.cookie("csrftoken")) },{% endcomment %} success:function (data) { console.log(data) },error:function (error) { console.log("error") console.log(error) } }) }); })</script>

后台函数:

from django.shortcuts import render,HttpResponseimport jsonclass Blog(): def login_in(req): if req.method == 'POST': data = json.loads(req.body) if data['user'] == "admin" and data['pwd'] == "123": return HttpResponse(json.dumps(data), content_type='application/json') return render(req, 'error.html')

Python Ajax请求及返回 json

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:EasySchedule定时任务web平台下一篇:

13 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18