Django Rest 框架:XMLHttpRequest 无法加载 http://127.0.0.1:8000/xyz/api/abc [英] Django Rest Framework: XMLHttpRequest cannot load http://127.0.0.1:8000/xyz/api/abc

查看:29
本文介绍了Django Rest 框架:XMLHttpRequest 无法加载 http://127.0.0.1:8000/xyz/api/abc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:在 url 末尾添加斜杠...

SOLUTION: Add a trailing slash to the end of the url...

"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 请求提取数据,但是我遇到了这个错误:

I have successfully created a Django Rest API and am able to store and host data locally it seems. I have built an angularjs1.0 app separately and am attempting to extract the data via $http get request however I'm running into this error:

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.

我已尝试安装 CORS 并将其添加到我的 INSTALLED_APPS,但似乎还没有任何效果.

I have attempted to install CORS and have added it to my INSTALLED_APPS, yet nothing seems to be working yet.

这是获取请求:

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);
            });
        },

看看我的 Django settings.py 文件:

Here's a look at my Django settings.py file:

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 = True

推荐答案

TL;DR

向附加斜杠的 URL 发出 AJAX 请求.

Issue your AJAX request to a slash-appended URL.

说明

经过我们的讨论,看来罪魁祸首是 Django 的自动 APPEND_SLASH = True,它在 CommonMiddleware 启用时被启用.

After our discussion, it appears that the culprit is Django's automatic APPEND_SLASH = True which is enabled when CommonMiddleware is enabled.

这会导致来自 Angular 应用的 AJAX 请求首先命中 301 Moved Permanently 重定向到附加斜杠的 URL.但是,corsheaders 中间件不会对此响应执行操作,因此浏览器会抱怨缺少 Access-Control-Allow-Origin 标头.

This causes the AJAX request from your Angular app to first hit a 301 Moved Permanently redirect to the slash-appended URL. However, the corsheaders middleware does not act on this response, so the browser complains about a missing Access-Control-Allow-Origin header.

这是通过直接请求附加斜杠的 URL 并完全绕过 301 重定向来解决的.

This is solved by requesting the slash-appended URL directly, and bypassing the 301 redirect altogether.

$http({
    method: 'GET',
    url: 'http://127.0.0.1:8000/xyz/api/abc/',  // trailing slash here
    cache: false
}).success(...);

这篇关于Django Rest 框架:XMLHttpRequest 无法加载 http://127.0.0.1:8000/xyz/api/abc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆