我有一个django视图,它在curl请求上返回HTTP 301:
grapefruit:~ pete$ curl -I http://someurl
HTTP/1.1 301 MOVED PERMANENTLY
Date: Fri, 16 Oct 2009 19:01:08 GMT
Server: Apache/2.2.9 (Win32) mod_wsgi/2.5 Python/2.6.2 PHP/5.2.6
Location: http://someurl
Content-Type: text/html; charset=utf-8
Run Code Online (Sandbox Code Playgroud)
我无法从卷曲中获取页面内容.但是,如果我使用浏览器访问该页面,我会按预期看到该内容.
有任何想法吗?
谢谢,皮特
我已经在Django中使用"django-cors"启用了CORS:
https://github.com/ottoyiu/django-cors-headers
按照此处的安装步骤后,我设置了以下内容:
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
'http://localhost:8000'
)
Run Code Online (Sandbox Code Playgroud)
django应用程序在http:// locahost:3000上运行
我的前端是一个Angular应用程序,它运行在"http:/ localhost:8000"上,我已经完成了以下更改以与django应用程序进行通信.
RestangularProvider.setBaseUrl('http://localhost:3000/');
Run Code Online (Sandbox Code Playgroud)
[为资源API使用Restangular]
当我调用GET API时,会发生"OPTIONS"飞行前调用,并且出现以下错误:
XMLHttpRequest无法加载http:// localhost:3000/users.凭据标志为"true",但"Access-Control-Allow-Credentials"标头为"".允许凭据必须为"true".因此不允许来源' http:// localhost:8000 '访问.
查看文档,我了解到我需要设置服务器期望作为调用的一部分的某些标头.所以,我添加了以下内容:RestangularProvider.setDefaultHeaders({"x-requested-with":'XMLHttpRequest'});
但是,在进行此更改时,我收到另一个错误,我无法解决: XMLHttpRequest无法加载http:// localhost:3000/users.预检的响应无效(重定向)
注意:请求/响应标头如下:
General:
Remote Address:127.0.0.1:3000
Request URL:http://localhost:3000/users
Request Method:OPTIONS
Status Code:301 MOVED PERMANENTLY
Response Headers
Access-Control-Allow-Headers:x-requested-with, content-type, accept, origin, authorization, x-csrftoken, user-agent, accept-encoding
Access-Control-Allow-Methods:GET, POST, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Origin:http://localhost:8000
Access-Control-Max-Age:86400
Content-Type:text/html; charset=utf-8
Date:Thu, 17 Dec 2015 11:10:16 GMT
Location:http://localhost:3000/users/
Server:WSGIServer/0.1 Python/2.7.10
X-Frame-Options:SAMEORIGIN
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, …Run Code Online (Sandbox Code Playgroud) 您好,我使用 django Rest Framework 作为 API 服务,使用 React 作为前端。在开发时以及部署到 iis 时,我可以从 React 调用 api。我收到此错误
请求的资源上不存在“Access-Control-Allow-Origin”标头。
我已经这样做了=> CORS错误
但我仍然遇到这个错误。
我的 Django 在 localhost:81 上运行
我的反应应用程序运行于:192.168.1.32:81
我可以在都安装的本地服务器上运行它,但是当我在另一台计算机上尝试时,出现此错误。
我使用 fiddler 4 覆盖主机名中的端口。
我正在开发一个以 Django Rest Framework 作为后端,React 作为前端的项目。当我最初在某个函数/视图中设置会话变量时,稍后当我尝试通过 axios 调用访问不同的视图时,如果我尝试访问之前创建的会话变量,则会在该视图中出现 KeyError。会话似乎没有存储。
我用谷歌搜索我遇到了我面临的类似问题。
我通过在 axios 调用中添加{ withCredentials: true }来遵循这个过程。现在我得到不同的错误。现在问题是无法访问后端。我收到一条错误消息,说Access to XMLHttpRequest at ' http://127.0.0.1:8000/url/ ' from origin ' http://localhost:3000 ' has been Blocked by CORS policy
我再次搜索了我遇到的问题,发现我必须在 django settings.py 中添加 CORS_ORIGIN_WHITELIST
我跟着下面的帖子
Django Python rest 框架,Chrome 中请求的资源上不存在“Access-Control-Allow-Origin”标头,可在 firefox 中使用
我已经像这样添加了 CORS_ORIGIN_WHITELIST
CORS_ORIGIN_WHITELIST = [' http://localhost:3000 ',' http: //127.0.0.1 :3000 ']
我仍然面临同样的问题。我不知道出了什么问题。任何人都可以帮助我解决这个问题。
谢谢你。
我在使用 Angular 6 应用程序中的 Django Rest Framework REST API 时遇到 CORS 问题。
该 API 在http://localhost:55098/admin上运行。当我用 Insomnia 调用它时它工作得很好。Angular 应用程序在http://localhost:4200上运行。
当我第一次输入http://localhost:4200/cgestores时,它应该重定向到http://localhost:55098/admin/cgestores,并且确实如此,但我收到了 CORS 错误消息(见下文)。
我的配置:
REST API:我使用Python 3.7(64位),Django(2.1.5)和Django Rest Framework(3.9.1)
我的设置.py:
ALLOWED_HOSTS = [
'testserver',
'localhost'
]
CORS_ORIGIN_ALLOW_ALL = True
INSTALLED_APPS = [
# Add your apps here to enable them
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_swagger',
'corsheaders',
'admin_v20',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'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', …Run Code Online (Sandbox Code Playgroud) 解决方案:在网址末尾添加一个尾部斜杠...
" http://127.0.0.1:8000/xyz/api/abc/ "而不是" http://127.0.0.1:8000/xyz/api/abc "
....
我已经成功创建了一个Django Rest API,并且能够在本地存储和托管数据.我已经单独构建了一个angularjs1.0应用程序,并试图通过$ http get request提取数据但是我遇到了这个错误:
XMLHttpRequest cannot load http://127.0.0.1:8000/xyz/api/abc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://172.20.9.163:8080' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
我试图安装CORS并将其添加到我的INSTALLED_APPS中,但似乎没有任何工作.
这是获取请求:
getABC : function() {
$http({
method: 'GET',
url: 'http://127.0.0.1:8000/xyz/api/abc',
cache: false
}).success(function(data) {
console.log(data)
callback(data);
});
},
Run Code Online (Sandbox Code Playgroud)
这是我的Django settings.py文件:
INSTALLED_APPS = (
'xyz',
'corsheaders',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
)
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',
)
CORS_ORIGIN_ALLOW_ALL …Run Code Online (Sandbox Code Playgroud)