【发布时间】:2016-08-19 01:15:23
【问题描述】:
基于w3schools ajax example,我正在尝试进行删除调用,然后从表中删除相应的行。这里有很多关于如何使用 JQuery 的答案,但我没有这样做。我找到了this answer,它让我这样写我的 JavaScript:
function deleteFullLicense(rowid, objectid) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 204) {
row = document.getElementById(rowid);
row.parentNode.removeChild(row);
}
else {
window.alert("Something went wrong. The delete failed.");
}
};
xhttp.open("POST", "deleteLicense/" + objectid, true);
xhttp.send({'csrfmiddlewaretoken': '{{ csrf_token }}'});
}
但我收到了Forbidden (CSRF token missing or incorrect.) 消息。我应该如何发送令牌?
【问题讨论】:
-
你需要将is设置为header。也许这可以帮助你:stackoverflow.com/questions/22063612/…
-
我尝试将其设置为标题使用:
xhttp.setRequestHeader("csrfmiddlewaretoken", '{{ csrf_token }}')但这没有区别。还有什么需要做的吗?
标签: javascript ajax django