gpt4 book ai didi

django - 预检请求不允许重定向

转载 作者:行者123 更新时间:2023-12-03 13:27:09 27 4
gpt4 key购买 nike

我遇到这个问题,在尝试使用休息 API 时收到响应:“从原点 ' https://kollektivet.app:8082/api/login/ ' 获取数据的访问已被 CORS 策略阻止:对预检请求的响应不会” t 通过访问控制检查:预检请求不允许重定向。”

https://kollektivet.app

当我尝试我正在使用的任何其他 api 时,就会发生这种情况。据我所知,这个错误意味着我正在尝试重定向,但我没有。

后端是 Django,如下所示:

    @csrf_exempt
@api_view(["POST"])
@permission_classes((AllowAny,))
def register(request,):
password = request.data.get("password", "")
email = request.data.get("email", "")
if not email and not password and not email:
return Response(
data={
"message": "username, password and email is required to register a user"
},
status=status.HTTP_400_BAD_REQUEST
)
new_user = User.objects.create_user(
email=email, password=password
)
return Response(status=status.HTTP_201_CREATED)

前端处于 React 状态,如下所示:

createUser(event) {
event.preventDefault();

let data = {
name: this.state.name,
password: this.state.password,
repeatPassword: this.state.repeatPassword,
email: this.state.email
};

if (this.state.name !== '' && this.state.password !== '' && this.state.email !== '' && this.checkPasswords()) {
console.log('name', this.state.name, 'password ', this.state.password, 'email ', this.state.email);
fetch("https://kollektivet.app:8082/api/register/", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
mode: "cors",
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
this.setState({message: "Du er nå registrert! For å aktivere din konto trykk på linken som vi har sendt til deg på epost"});
this.setState({name: ""});
this.setState({password: ""});
this.setState({repeatPassword: ""});
this.setState({email: ""});

}
}

我确实有这个 Django 设置文件:

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)

如果相关的话,我正在 apache2 上运行它。8082端口也被关闭。在同一服务器上时需要打开吗?

谢谢!

最佳答案

您将被重定向到site.site.comapi/register/

你还有其他中间件可以做到这一点吗?也许在 Apache 配置中?

请注意,这是一个301,因此您的浏览器已缓存此响应,并且现在将始终重定向到那里,即使您浏览了导致此重定向的代码,或者即使您阻止 Django 运行。

因此您还需要清除浏览器中的重定向缓存。

这就是我不喜欢 301 回复的原因。 302更有礼貌。

关于django - 预检请求不允许重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53215045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com