[英]Django - Javascript - Csrf Token
這是我嘗試嘗試將csrf令牌添加到javascript表單的代碼。
function save() {
var form = document.createElement("form");
console.log(form);
form.setAttribute('method', 'post');
form.setAttribute('action', '/quiz_score/');
document.body.appendChild(form);
var i = document.createElement("input");
i.setAttribute('name', 'Score');
i.setAttribute('value', ""+score);
i.setAttribute('name', 'csrfmiddlewaretoken');
i.setAttribute('value', {% csrftoken %});
form.appendChild(i);
form.submit();
}
您能看到任何問題嗎? 它有一個錯誤,因此JS無法運行。
您的代碼中有2個錯誤。 看代碼的簡單方法。 試試吧:
var i = document.createElement("input");
i.setAttribute('name', 'Score');
i.setAttribute('value', ""+score);
form.appendChild(i);
var i = document.createElement("input");
i.setAttribute('name', 'csrfmiddlewaretoken');
i.setAttribute('value', '{{ csrf_token }}');
form.appendChild(i);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.