简体   繁体   English

从API REST Django和Angular检索数据

[英]Retrieve data from API REST Django and Angular

I'm trying to retrieve the json from the rest api set up using Django. 我正在尝试从使用Django设置的其余api中检索json。

This data is currently only hosted on: http://127.0.0.1:8000/xyz 该数据当前仅托管于: http : //127.0.0.1 : 8000/xyz

When I try to retrieve it using 当我尝试使用

 $http({
            method: 'GET',
            url: 'http://127.0.0.1:8000/xyz',
 })

I get an error that is: 我得到的错误是:

XMLHttpRequest cannot load http://127.0.0.1:8000/xyz . XMLHttpRequest无法加载http://127.0.0.1:8000/xyz No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://127.0.0.1:8080 ' is therefore not allowed access. 因此,不允许访问源' http://127.0.0.1:8080 '。

Can someone tell me a way of dealing with this, please? 有人可以告诉我一种处理方法吗?

Thanks! 谢谢!

Here's my Django settings folder: 这是我的Django设置文件夹:

INSTALLED_APPS = (
    'student',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    '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',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware'
)

CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_URLS_REGEX = r'^/api.*$'
CORS_ORIGIN_WHITELIST = (
    'mydomain',
    'localhost:3000',
    'http://127.0.0.1:8000/'
)

Look into using django-cors-headers to have Django return the proper headers. 考虑使用django-cors-headers使Django返回正确的头。 You can then create a whitelist for your site ( http://127.0.0.1:8080 for development and whatever your final domain for production) 然后,您可以为您的网站创建白名单( http://127.0.0.1:8080用于开发,以及您最终用于生产的任何域)

I use the following on my settings for a similar setup: 我将以下设置用于类似的设置:

INSTALLED_APPS += ('corsheaders',)
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_URLS_REGEX = r'^/api.*$'
CORS_ORIGIN_WHITELIST = (
    'mydomain',
    'localhost:3000',
)

You may also need to add the following to your Angular project: 您可能还需要将以下内容添加到Angular项目中:

$http.defaults.useXDomain = true;

[UPDATE] [UPDATE]

See this blog for more details 请参阅此博客以获取更多详细信息

Apart from the changes in settings.py, please try to add a slash at the end of the url you are calling 除了settings.py的更改之外,请尝试在要调用的网址末尾添加斜杠

    $http({
                method: 'GET',
-                url: 'http://127.0.0.1:8000/xyz',
+                url: 'http://127.0.0.1:8000/xyz/',
     })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM