javascript - 在 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',
]
但是,错误仍然存在。我对导致错误的原因有误吗?有什么建议么?我真的很感激。
解决方案
推荐阅读
- android - 在android studio中打开新项目时android资源编译失败
- r - 按唯一标识符折叠多行,多个值(列),每个变量的多个元素(行)
- jquery - 获取每个 .class 的编号
- c++ - 为什么按值传递局部变量有效?
- c# - XPathNavigator 可以从测试输入中选择,但不能从真实输入中选择
- c# - C# - 使用 String.Split(CharArray) 时识别匹配字符
- python - Python Dash:'Dash' 对象没有属性'route'
- css - 在全屏桌面视图中如何隐藏 fontawesome 图标
- ruby-on-rails - 如何去除空的段落标签
- python - 在 BeautifulSoup 中提取带有换行符的文本