前后端不分离中使用 Ajax 请求(Django3.2)

博客地址:https://www.cnblogs.com/zylyehuo/

view.py

def get_tokens(request):
	token = get_token(request)
	return HttpResponse(token)

login.html

$.ajax({
	url:"/get tokens/"
    success: function (res{
    	console.log(res);
		localStorage.setItem("token",res)
	}
});

$(".login btn").click(function (){
    $.ajax({
        url:"/login/"
        type:"post"
        data:{
            csrfmiddlewaretoken:localStorage.getItem("token")
            user: $(".user").val(),
            pwd: $(".pwd").val(),
        },
        success: function (res) {
            console.log("res:",res);
            if(res,state){
                location.href = "/index/"
            }else {
                $(".err").html(res.msg)
            }
        }
    }
}
posted @ 2023-07-21 10:58  zylyehuo  阅读(48)  评论(0编辑  收藏  举报