我想把删除请求从前端(Rep)发送到后端(Django REST Framework),但是它失败了,没有多少信息。它在Chrome中工作正常,但在Safari中失败。它也适用于GET和POST请求,唯一的问题是POST请求只在第二次尝试中才能工作(同样也只在Safari中)。
I've tried different CORS configurations but no luck.
Django CORS配置:
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
],
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
}
CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
'localhost:3000',
'http://localhost:3000',
)
CORS_TRUSTED_ORIGINS = (
'localhost:3000',
'http://localhost:3000',
'*'
)
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',
'access-control-allow-credentials',
'access-control-allow-headers',
'access-control-allow-methods',
'access-control-allow-origin',
)
Frontend code:
fetch('http://localhost:8000/api-v1/projects/'.concat(this.props.projectId),
{
method: 'DELETE',
mode: 'cors',
credentials: 'same-origin',
body: {},
redirect: 'follow',
referrer: 'no-referrer',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Access-Control-Allow-Headers': '*, Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
'Access-Control-Allow-Credentials': true,
'Authorization': 'Basic ' + btoa('admin:pass'),
},}).catch( e => console.error(e));
Django logs shows that DELETE request received and the response code is 301. Safari shows this in the console:
[Error] The network connection was lost.
[Error] Fetch API cannot load http://localhost:8000/api-v1/projects/54/ due to access control checks.
[Error] Failed to load resource: The network connection was lost.
[Error] TypeError: The network connection was lost.
promiseReactionJob
发布于 2019-06-25 10:40:12
在URL末尾的异常尾随斜线。我花了5个小时进行故障排除,但解决方案是在URL的末尾添加一个符号:)
https://stackoverflow.com/questions/-100007047
复制相似问题