我正在使用ajax调用以下URL:
“https://www.bittrex.com/api/v1.1/public/getticker?market=usdt-btc”
我收到以下错误:
请求的资源上不存在“Access-Control-Allow-Origin”标头。
我已经安装了 django-cors-headers ,我的 settings.py 看起来像这样:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'crispy_forms',
# The following apps are required:
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'http://127.0.0.1:8000',
)
为什么我仍然收到错误?
我已阅读与此相关的所有其他答案,但它们对我没有帮助。我也使用过 Chrome 插件,它确实可以使用它,但我想要一个没有它的解决方案。
Django 版本 = 1.8
配置看起来不错并且根据文档。我刚刚和我的配置进行了比较,几乎是一样的。由于我仅将 API 用于一个网站,因此我已将其配置在我的 CORS_ORIGIN_WHITELIST 中,您也可以尝试一下,看看它是否有响应。
另外,尝试删除所有Python缓存文件并重新启动应用程序服务器,也许是未加载的设置。
这不是您的应用程序问题,即 django 问题,因为 CORS 是一个 http 标头,它让用户代理获得对与您的域不同的域中的资源的权限。这是一个浏览器问题,因此在 django 中添加 CORS 应用程序将不起作用。 更多信息请点击这里 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
尝试在ajax请求中添加crossDomain: true