【发布时间】:2021-05-01 12:03:42
【问题描述】:
我在其他关于 CORS 和 django 的帖子中找不到我的答案。因此,经过一整天的时间弄清楚我做错了什么,我在这里问我的问题。
我的前端是 VUE.js,这个应用正在使用 Django 和 djangoRestFramework 在 DigitalOcean.com 应用服务器上对我的后端进行 API 调用。
fetch( 'https://someurl.ondigitalocean.app/api/user/token',
{ method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: 'emai@email.com', password: 'password' }),
},
).then(function(response){
if (response.ok){
return response.json()
}
}).then(function(data) {
console.log(data)
}
)
这会发布一个接收令牌的帖子。此调用仅用于测试目的。 API 工作正常,为了测试,我使用默认的 django 浏览器界面,而且我的所有单元测试都正常。
我使用 django-cors-headers 作为中间件,并在我的设置文件中将 corsheaders 添加到我的“APP”和“MIDDLEWARE”中。
这些是我的设置:
CORS_ALLOWED_ORIGINS = [
"http://maf-internet.nl",
"http://localhost:8080",
"http://127.0.0.1:8000"
]
CSRF_TRUSTED_ORIGINS = [
"http://maf-internet.nl",
]
CORS_ALLOW_ALL_ORIGINS = 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',
]
这些是我的包裹:
Django>= 2.2, <2.2.2
djangorestframework>=3.9.0,<3.10.0
psycopg2-binary
dj-database-url
gunicorn
django-cors-headers>=3.7.0
flake8>=3.6.0,<3.7.0
Digital Ocean 也有一个 CORS 起源的条目。我这里加了前端域,结果还是一样。在这里你可以看到我的错误 405。
我只是不明白我做错了什么。可能是什么问题?
【问题讨论】:
标签: django django-cors-headers