簡體   English   中英

如何更正 django 中的以下代碼,它給出錯誤“禁止(CSRF 令牌丟失或不正確。)”

[英]How do I correct the following code in django, It's give an error “Forbidden (CSRF token missing or incorrect.)”

<script>
 var CSRF_TOKEN = '{{ csrf_token }}';
    function getQuestion(str)
    {

        if(str!="select subject")
    {
        var req=new XMLHttpRequest();


        req.open("post","/addQuestion/",true);
        str=encodeURIComponent(str);
        req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        req.send("subject="+str);
        req.onreadystatechange=function()
        {
            if(req.status==200 && req.readyState==4)
            {
                document.getElementById("formdiv").innerHTML=req.responseText;
                document.getElementById("formdiv").style.overflowY="scroll";

            }
        }

    }
    }
</script>

您應該在請求 header 中設置 CSRF_TOKEN

req.setRequestHeader('CSRF_TOKEN', CSRF_TOKEN);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM