在尝试使用rest api时,我遇到了这样的问题:“CORS策略阻止了对来自源'https://kollektivet.app‘的'https://kollektivet.app:8082/api/login/’fetch的访问:对印前检查请求的响应没有通过访问控制检查:不允许对印前检查请求进行重定向。”
Picture of response when trying to fetch
当我尝试我正在使用的任何rest 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也关闭。当它在同一台服务器上时,是否需要打开它?
谢谢!
发布于 2018-11-09 00:29:08
您将被重定向至site.site.comapi/register/
你有没有其他的中间件可以做到这一点?也许在Apache配置中?
注意,它是一个Django 301,所以你的浏览器已经缓存了这个响应,并且即使你漫游了导致这个重定向的代码,或者即使你停止了Django的运行,它也会一直重定向到那里。
因此,您还需要清除浏览器中的重定向缓存。
这就是我不喜欢301响应的原因。302要礼貌得多。
发布于 2020-08-07 08:30:12
只是在这里写我的故事,以防有人和我有同样的问题。
基本上,这是一个服务器端问题,所以不需要在前端进行任何更改。
对于此错误消息,它指示OPTIONS请求得到的响应状态代码为301 Moved Permanent“、"307 Temporary Redirect”或"308 Permanent Redirect“。
请使用OPTIONS响应中的'location‘属性检查您请求的URL,看看它们是否相同。
对我来说,问题是我的请求URL是**"https://server.com/getdata"**
,但我在服务器上设置的URL是**"https://server.com/getdata/"**
然后服务器给我一个308,用'/‘来纠正它,这适用于POST,GET和HEAD,但不适用于选项。
我用的是flask、flask_restful和flask_cors。
使用这个也可以为我解决这个重定向问题。
app.url_map.strict_slashes = False
发布于 2019-04-24 17:53:24
我也有过同样的问题,直到我发现重定向是由Django国际化框架引起的,在这个框架中,当实际请求path_to_resource
时,所有的url都会得到一个i18n url扩展名,比如/en/path_to_resource
。国际化框架通过302重定向实现了这一点。
这个问题的解决方案是将rest-api放在带有i18n_patterns的部分之外。然后,生成的urls.py可能如下所示
urlpatterns = [
path('i18n/', include('django.conf.urls.i18n')),
path('rest/', include(('rest.urls', 'rest'), namespace='rest')),
]
urlpatterns += i18n_patterns(
path('admin/', admin.site.urls),
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
path('my_app/', include(('my_app.urls', 'my_app'), namespace='my_app')),
)
https://stackoverflow.com/questions/53215045
复制