首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用fetch的DELETE请求在Safari中不起作用,但在Chrome中有效

使用fetch的DELETE请求在Safari中不起作用,但在Chrome中有效
EN

Stack Overflow用户
提问于 2019-06-25 01:07:57
回答 1查看 0关注 0票数 0

我想把删除请求从前端(Rep)发送到后端(Django REST Framework),但是它失败了,没有多少信息。它在Chrome中工作正常,但在Safari中失败。它也适用于GET和POST请求,唯一的问题是POST请求只在第二次尝试中才能工作(同样也只在Safari中)。

I've tried different CORS configurations but no luck.

Django CORS配置:

代码语言:txt
复制
    '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:

代码语言:txt
复制
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:

代码语言:txt
复制
[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
EN

Stack Overflow用户

发布于 2019-06-25 10:40:12

在URL末尾的异常尾随斜线。我花了5个小时进行故障排除,但解决方案是在URL的末尾添加一个符号:)

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档