Django cors标头不适用于Expo web

Django cors标头不适用于Expo web,django,cors,expo,django-cors-headers,react-native-web,Django,Cors,Expo,Django Cors Headers,React Native Web,我刚刚在http://192.168.0.6:19006,出现了许多问题 当我没有安装django cors头文件时,只加载了主页,其他请求都失败了。我很快意识到我必须安装django cors头文件。所以我做了。但是我的web应用程序无法保持登录状态。登录过程本身在服务器端是成功的。客户端收到一条消息,告知登录成功。但当它转换到下一页时,它会自动返回到主页(如我设置的),因为应用程序无法保持登录状态。我假设cookie凭证有问题。但我将凭据设置如下所示: CORS_ORIGIN_WHITELI

我刚刚在
http://192.168.0.6:19006
,出现了许多问题

当我没有安装django cors头文件时,只加载了主页,其他请求都失败了。我很快意识到我必须安装django cors头文件。所以我做了。但是我的web应用程序无法保持登录状态。登录过程本身在服务器端是成功的。客户端收到一条消息,告知登录成功。但当它转换到下一页时,它会自动返回到主页(如我设置的),因为应用程序无法保持登录状态。我假设cookie凭证有问题。但我将凭据设置如下所示:

CORS_ORIGIN_WHITELIST = [
    'http://192.168.0.6:19006',
]
CORS_ALLOW_CREDENTIALS = True
SESSION_COOKIE_SAMESITE = None

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
...
]
INSTALLED_APPS = [
    ...
    'corsheaders',
]
另一个问题是静态文件没有提供CORS允许的头。即使我使用django cors头并允许所有设置,静态文件也无法加载,并显示错误消息:

Access to XMLHttpRequest at 'http://192.168.0.6:8000/static/app%20Terms%20of%20Service.json' from origin 'http://192.168.0.6:19006' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

为什么
CORS\u ALLOW\u CREDENTIALS=True
不起作用?还是我遗漏了什么?

有很多问题

首先,对于客户端来说,问题在于axios

当您登录并使用cookie时,应该为axios设置
with credentials:true

接下来,静态文件不会经过Django中间件,所以您必须手动设置静态文件的CORS头

#vevn\Lib\site-packages\django\views\statics.py

content_type, encoding = mimetypes.guess_type(str(fullpath))
content_type = content_type or 'application/octet-stream'
response = FileResponse(fullpath.open('rb'), content_type=content_type)
response["Last-Modified"] = http_date(statobj.st_mtime)

# cors middleware hook
from corsheaders.middleware import CorsMiddleware
corsMiddlewareInstance = CorsMiddleware()
corsMiddlewareInstance.process_response(request, response)

if encoding:
    response["Content-Encoding"] = encoding
return response
最后,我的
组件没有显示在react native web上。原因是,
aspectRatio
在react native web上不受支持。您必须根据自己的比例手动设置
宽度
高度

<View onLayout={event => this.setState({
                                width: event.nativeEvent.layout.width
                            })}
    >
<Image source={{ uri: this.props.image.uri }}
                style={{
                        width: this.state.width,
                        height: this.state.width ? this.state.width * this.props.image.height / this.props.image.width : null,
this.setState({
宽度:event.nativeEvent.layout.width
})}
>

设置
CORS\u ORIGIN\u ALLOW\u ALL
在您的设置中为True是否有效?@JulienKieffer否,我已经尝试过该设置,但不幸的是它对我无效。我猜问题出在客户端,而不是Django cors头,因为我使用的是Apollo websocket。我可以看到头被设置为Access Control Allow Credentials:true Access Control Allow Origin:和set Cookie:。。。因此,登录成功并发送了cookie。但它不适用于阿波罗websocket。