我在尝试将 Django 服务器连接到 axios 时遇到问题。这应该是一个简单的修复,但我被困住了!
我从 Django 服务器收到此错误:
[23/Aug/2021 19:25:36] "POST /subscription HTTP/1.1" 403 2519
Forbidden (CSRF token missing or incorrect.): /subscription
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的方法:
const newsletterSignUp = async function (email) {
try {
let res = await axios({
method: "post",
url: "http://127.0.0.1:8000/subscription",
data: { email: email },
});
return res;
} catch (err) {
console.error(err);
return err;
}
Run Code Online (Sandbox Code Playgroud)
我尝试添加自定义标头,但我认为名称中的破折号引起了问题,而且我不知道如何解决它。
headers: { set-cookie: "csrftoken=ee95ec102d0d884ea95eb09cb421cdd8382aed79" }
Run Code Online (Sandbox Code Playgroud)
我知道我的 Django 代码很好,因为它可以在浏览器中运行。我已附上它以供参考。
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Email subscriptions</title>
<!-- Bootstrap --> …Run Code Online (Sandbox Code Playgroud)