简体   繁体   English

被 CORS 策略阻止:No"Access-Control-Allow-Origin" Using Flask

[英]Blocked by CORS policy: No"Access-Control-Allow-Origin" Using Flask

I am trying to make a flask web application that can download a whole web page and render it on localhost to crop elements.The requests module takes all of the data from the site except some as it says我正在尝试制作一个 flask web 应用程序,它可以下载整个 web 页面并将其呈现在本地主机上以裁剪元素。请求模块从站点获取所有数据,除了它所说的一些数据

from origin 'http://127.0.0.1:5000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.来自 origin 'http://127.0.0.1:5000' 已被 CORS 策略阻止:请求的资源上不存在 'Access-Control-Allow-Origin' header。

Following is the code that I have for the flask app.以下是我为 flask 应用程序准备的代码。 I have tried flask_cros too but it doesn't seem to help.我也试过 flask_cros 但它似乎没有帮助。

from flask_socketio import SocketIO, emit
from flask_session import Session
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from flask_cors import CORS,cross_origin
app= Flask(__name__)
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
app.config["CORS_HEADERS"]="Content-Type"
socketio=SocketIO(app)
session=Session(app)
cors = CORS(app)
dir= os.path.abspath(os.getcwd())
@app.route("/")
def index():
   
   return render_template("index.html")
@app.route("/crop",methods=["POST"])
@cross_origin()
def crop():
   if request.method=="POST":
       outfile=open("result.txt","w")
       outfile.write("<iframe>")
       outfile.close()
       dummy= open("templates/down.html",'w')
       dummy.close()
       global link
       link = str(request.form.get("url"))
       
       r= requests.get(link,allow_redirects=True,headers={"User-Agent":"Chrome/42.0.2311.135"})
       if r.status_code==405:
           return render_template("exception.html")
       soup = BeautifulSoup(r.content,"html.parser")
       outfile=open("links.html","w")
       for a in soup.findAll("link",attrs={'href':True},rel="stylesheet"):
           if a['href'][0:4]!="http":
               dom= urlparse(link)
               wurl=dom.scheme+"://"+dom.netloc
               r1=requests.get(wurl+a['href'],headers={"User-Agent":"Chrome/42.0.2311.135"})
               if r1.status_code==200:    
                   a['href']= wurl + a['href']

                   print(a)
                   outfile.write(str(a))
               else:
                    a['href']= link + a['href']
                    outfile.write(str(a))
           else:
               outfile.write(str(a))
           
       outfile.close()
       
      
       body= soup.prettify("utf-8")
    
       with open("templates/down.html",'wb') as file:
           file.write(body)
       return render_template("base.html") ```

You want to have flask send that missing header:您想让 flask 发送丢失的 header:

Access-Control-Allow-Origin: *

暂无
暂无

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

相关问题 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 Axios blocked by CORS policy with Django REST Framework: 'Access-Control-Allow-Origin' header in the response must not be wildcard - Axios blocked by CORS policy with Django REST Framework: 'Access-Control-Allow-Origin' header in the response must not be wildcard Javascript XMLHttpRequest-已被CORS策略阻止:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Javascript XMLHttpRequest - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略已阻止来自原点“null”:请求的资源上不存在“Access-Control-Allow-Origin”header - From origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 政策阻止了获取“url”的访问:请求的资源上不存在“Access-Control-Allow-Origin”header。 ReactJS - Access to fetch `url` been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. ReactJS Flask CORS - 重定向上没有Access-control-allow-origin标头() - Flask CORS - no Access-control-allow-origin header present on a redirect() Webfaction已被CORS策略阻止从来源“ http:// mydomain”访问domain / fonts / fontname.ext的字体:No&#39;Access-Control-Allow-Origin&#39; - Webfaction Access to font at domain/fonts/fontname.ext from origin 'http://mydomain' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' Flask / Flask-CORS:缺少CORS标头“ Access-Control-Allow-Origin” - Flask/Flask-CORS: CORS header ‘Access-Control-Allow-Origin’ missing 由于没有 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 &amp; javascript fetch():CORS 政策:没有“访问控制允许来源”Z099FB995346F31C79E3ZF6 存在 - django & javascript fetch(): CORS policy: No 'Access-Control-Allow-Origin' header is present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM