您好,登錄后才能下訂單哦!
前臺表單:
<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,HttpResponse
import json
class 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')
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。