首页 > 解决方案 > 在 iframe 中嵌入 django 登录

问题描述

我需要在 Amazon MTurk 上进行一项研究,我想创建一个一次性 django 用户并为他们登录(所以我们不需要用户自己注册和登录,这是一项一次性研究,所以他们不需要记住他们的用户名或密码)。我使用了一系列 jquery ajax 调用来实现这个目标:

<consent.html>
function startGame() {
            $.ajax({
                url:'https://comphcithree.eecs.umich.edu:8030/chess1/create_user/',
                dataType: 'json',
                type: 'POST',
                success: function(data) {
                    console.log('username:', data.username);
                    console.log('password:', data.password);
                    $.ajax({
                        url:'https://comphcithree.eecs.umich.edu:8030/login/',
                        data: {
                            'username': data.username,
                            'password': data.password,
                            'csrfmiddlewaretoken': csrftoken
                        },
                        dataType: 'json',
                        type: 'POST',
                        error: function() {
                            console.log('logged in successfully');
                            window.location.href = 'https://comphcithree.eecs.umich.edu:8030/start/';
                        }
                    });
                }
            });

当我通过 django URL 访问页面时,这工作正常。但是由于我需要在 MTurk 上部署它,我需要将页面放在 HTML iframe 中,它停止工作。这是我访问页面的方式(consent.html):

<iframe width="1000px" height="600px" src="https://comphcithree.eecs.umich.edu:8030/chess1/consent"></iframe>

我的状态为 403 禁止,并且User对象(应该)变为AnonymousUser

在此处输入图像描述

我检查了用户是否在我们的数据库中成功创建。然后我以为问题是CORS引起的,我尝试django-cors-header在我的settings.py中添加。这是我添加的内容:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'chess1',
    'django_mysql',
    'accounts.apps.AccountsConfig',
    'django_extensions',
    'corsheaders',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]

X_FRAME_OPTIONS = 'ALLOWALL'
# CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOWED_ORIGINS = [
    'http://localhost:63342/',
]
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = [
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
]
CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
]

但是,错误仍然存​​在。我对导致错误的原因有误吗?有什么建议么?我真的很感激。

标签: javascriptdjangohttp-status-code-403

解决方案


推荐阅读