[英]flask-cors Control-Allow-Origin'
Trying to execute simple json request on local server combined with Python and django. 尝试在结合Python和django的本地服务器上执行简单的json请求。
my apiview.py 我的apiview.py
from rest_framework import generics
from django.shortcuts import get_object_or_404
from .jsonserializer import GroupSerializer, SubgroupSerializer, ProductsSerializer
from .models import pGroups, pSubgroups, Products
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/Group/")
# @cross_origin()
# Create your views here.
class GroupList(generics.ListCreateAPIView):
queryset = pGroups.objects.all()
serializer_class = GroupSerializer
Jquery script jQuery脚本
let dropdown = $('#locality-dropdown');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Choose product group</option>');
dropdown.prop('selectedIndex', 0);
const url = 'http://127.0.0.1:8000/Group/';
// Populate dropdown with list of provinces
$.getJSON(url, function (data) {
$.each(data, function (key, entry) {
console.log(entry.name);
dropdown.append($('<option></option>').attr('value', entry.abbreviation).text(entry.name));
})
});
And after all i log in console smthng like : 毕竟我登录控制台smthng像:
Failed to load http://127.0.0.1:8000/Group/ : No 'Access-Control-Allow-Origin' header is present on the requested resource. 无法加载http://127.0.0.1:8000/Group/ :请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access. 因此,不允许访问原始“空”。
What i'm trying is : Flask cors but seems im missing something in apiview.py . 我正在尝试的是: Flas cors,但我似乎在apiview.py中缺少某些内容。
PS I'm complete beginner in web. PS我是网络方面的完整初学者。 Edit: With postman i get the response 编辑:与邮递员我得到答复
[
{
"id": 1,
"group_chiper": "01",
"group_shortcut": "DRN",
"group_name": "Drink",
"operator": 2
},
{
"id": 2,
"group_chiper": "02",
"group_shortcut": "ML",
"group_name": "Meals",
"operator": 1
}
]
Based on Robin comment : 根据罗宾评论:
django-cors-headers is one of approaches. django-cors-header是其中一种方法。
Using command line install it to your root project : 使用命令行将其安装到您的根项目:
pip install django-cors-headers
If you are using environment you may install to it root project folder and reset the environment. 如果您正在使用环境,则可以将其安装到根项目文件夹并重置环境。
Add the installed app to your 将已安装的应用添加到您的
settings.py settings.py
INSTALLED_APPS = (
...
'corsheaders',
...
)
MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
CorsMiddleware should be placed as high as possible, especially before any middleware that can generate responses such as Django's CommonMiddleware or Whitenoise's WhiteNoiseMiddleware. CorsMiddleware应该放置在尽可能高的位置,尤其是在可以生成响应的任何中间件之前,例如Django的CommonMiddleware或Whitenoise的WhiteNoiseMiddleware。 If it is not before, it will not be able to add the CORS headers to these responses. 如果不是以前,它将无法将CORS标头添加到这些响应中。
The part i was missing is to add in settings.py 我缺少的部分是添加settings.py
line of code 代码行
CORS_ORIGIN_ALLOW_ALL = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.