Django drf jwt token认证前后端使用流程
async function login(username, password) {
const response = await fetch('http://localhost:8000/api/token/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
password: password,
}),
});
const data = await response.json();
if (response.ok) {
// 保存 Access Token 和 Refresh Token
localStorage.setItem('access_token', data.access);
localStorage.setItem('refresh_token', data.refresh);
} else {
console.log('Login failed:', data);
}
}