无法从 Django API 获取 Set-Cookie 标头



当我尝试使用Django API时,我遇到了这些错误。 我已经将ReactJS用于前端,DjangoREST用于后端。

当 withCredentials 设置为 False 时,我收到此错误。

无法加载 https://retailo-dot-dogwood-keep-190311.appspot.com/admin/:预检响应中的访问控制-允许-标头不允许请求标头字段访问控制-允许-源。 登录表单.jsx:58

如果为 True,我收到此错误。

无法加载 https://retailo-dot-dogwood-keep-190311.appspot.com/admin/:对预检请求的响应未通过访问控制检查:当请求的凭据模式为"include"时,响应中"访问控制-允许源"标头的值不得为通配符"*"。因此,不允许访问源"http://localhost:3000"。由 XMLHttpRequest 发起的请求的凭据模式由 withCredentials 属性控制。

我使用过Axios,这是我的代码。

axios.get('https://retailo-dot-dogwood-keep-190311.appspot.com/admin/',{ 
// withCredentials: true,
headers: {
'Access-Control-Allow-Origin': 'include',
'Content-Type': 'application/json',
},
// credentials: 'same-origin',
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

这不是 React 的错误。这是你必须在 Django 设置中更改。 你已经允许来自你的Django端的CORS。

https://github.com/ottoyiu/django-cors-headers

请查看上面的链接。根据此设置更改设置。

最新更新