简体   繁体   English

CORS header 'Access-Control-Allow-Origin' 缺失,尽管 header 从那里开始

[英]CORS header 'Access-Control-Allow-Origin' missing despite header begin there

I'm making an api in flask-restful, and I'm trying to make a request, but when I try I get the error:我正在烧瓶中制作 api,我正在尝试提出请求,但是当我尝试时出现错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:5000/events/. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

My code is:我的代码是:

class Event(Resource):
    def get(self, id=0):
        if id == 0:
            return random.choice(events), 200
        for event in events:
            if(event["id"] == id):
                return event, 200, {'Access-Control-Allow-Origin': '*'}
        return "Event not found", 404

I have added the header, but I stil get the error.我已经添加了 header,但我仍然收到错误。 Does anyone know what the problem is?有谁知道问题是什么? Also, if anyone needs it, the full code is here:另外,如果有人需要,完整的代码在这里:

from flask import Flask
from flask_restful import Api, Resource, reqparse
import random
app = Flask(__name__)
api = Api(app)

events = [
    {
        "id": 0,
        "starter": "a",
        "dungeon": "test a",
        "info": "A test event for the web app and api"
    },
    
    {
        "id": 1,
        "starter": "b",
        "dungeon": "test b",
        "info": "A test event for the web app and api 2"
    }
]

class Event(Resource):
    def get(self, id=0):
        if id == 0:
            return random.choice(events), 200
        for event in events:
            if(event["id"] == id):
                return event, 200, {'Access-Control-Allow-Origin': 'file:///C:/Python/website%20test/index.html'}
        return "Event not found", 404

api.add_resource(Event, "/events", "/events/", "/events/<int:id>")
if __name__ == '__main__':
    app.run(debug=True)

Edit: Here is how I am making the request:编辑:这是我提出请求的方式:

let request = new XMLHttpRequest();
request.open("GET", "http://127.0.0.1:5000/events/");
request.send();
request.onload = () => {
    if (request.status == 200) {
        console.log(JSON.parse(request.response));
    } else {
        console.log('API offline');
    }
}

install flask-CORS package.安装烧瓶-CORS package。

pip install -U flask-cors

and apply the following changes.并应用以下更改。 On your current code.在您当前的代码上。

from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/")
def helloWorld():
  return "Hello, cross-origin-world!"

暂无
暂无

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

相关问题 Flask / Flask-CORS:缺少CORS标头“ Access-Control-Allow-Origin” - Flask/Flask-CORS: CORS header ‘Access-Control-Allow-Origin’ missing “请求 header 字段 Access-Control-Allow-Origin 在预检响应中被 Access-Control-Allow-Headers 不允许”尽管 CORS 配置有效 - “Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response” despite valid CORS config Flask CORS - 重定向上没有Access-control-allow-origin标头() - Flask CORS - no Access-control-allow-origin header present on a redirect() 发布到python eve时缺少CORS标头“ Access-Control-Allow-Origin” - CORS header ‘Access-Control-Allow-Origin’ missing when posting to python eve AWS 对 XMLHttpRequest 的访问已被 CORS 策略阻止:No 'Access-Control-Allow-Origin' header - AWS Access to XMLHttpRequest at from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header 由于没有 Access-Control-Allow-Origin 标头但标头存在而被 CORS 阻止的 HTTP 请求 - HTTP request blocked by CORS for not having Access-Control-Allow-Origin header but the header is present Django,Next.JS:CORS header 'Access-Control-Allow-Origin' 丢失,CORS 请求没有成功,即使定义了 django-cors-headers - Django, Next.JS: CORS header ‘Access-Control-Allow-Origin’ missing, CORS request did not succeed even though django-cors-headers is defined 如何使Cloudfiles FormPost返回“Access-Control-Allow-Origin”标头以启用CORS? - How do I make Cloudfiles FormPost return the “Access-Control-Allow-Origin” header to enable CORS? 指向托管 Django 服务器的本地 React 应用程序 CORS 没有“访问控制允许来源”header - Local React app pointing to hosted Django server CORS no 'Access-Control-Allow-Origin' header django-cors-headers 不工作:请求的资源上不存在“Access-Control-Allow-Origin”header - django-cors-headers not working: No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM