【发布时间】:2018-02-28 07:29:10
【问题描述】:
当我在同一页面中尝试 ajax 到 html 时,它可以工作。像这样;
<html>
<head>
...
</head>
<body>
....
<script>
$.ajax({
url: /test/,
method: 'POST',
headers: {'X-CSRFToken': '{{ csrf_token }}'},
data: { name: a, surname: b},
dataType: 'json',
success: function (data) {
getList(data);
}
});
</script>
</body>
</html>
当我尝试调用与外部相同的 javascript 时。它不起作用。为什么?
<html>
<head>
...
</head>
<body>
....
<script src="{% static 'js/test.js' %}"></script>
</body>
</html>
【问题讨论】:
-
你在django中配置好静态文件了吗?
-
静态文件配置正确。我有 403 错误。
-
The docs have a solution for this 自动将 CSRF 令牌添加到所有 AJAX 调用中。
标签: ajax django http-status-code-403