Eggs Sunny Side Up
본문 바로가기
프레임워크(Framework)/Flask

has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

by guswn100059 2023. 8. 5.

 

Flask와 Spring 서버 간의 CORS 오류가 일어날 때

from flask      import Flask, request, jsonify, current_app, render_template, redirect, url_for
from flask.json import JSONEncoder
from flask_mail import Mail, Message
import glob
from flask_cors import CORS

app = Flask(__name__)
mail = Mail(app)

app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'moondansok66@gmail.com'
app.config['MAIL_PASSWORD'] = 'unqojkesjiynxogd'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

CORS(app)
@app.route("/connectMain", methods = ["POST"])
def test():
    u_id = request.form.get('login_id')
    print(u_id)
    lst = mainpage(u_id)
    print(lst)
    # BigInt 데이터타입 => str로 변환
    for i in range(len(lst)):
        lst[i]['sah_in_at'] = str(lst[i]['sah_in_at'])
        lst[i]['sah_out_at'] = str(lst[i]['sah_out_at'])
        lst[i]['sac_in_at'] = str(lst[i]['sac_in_at'])
        lst[i]['sac_out_at'] = str(lst[i]['sac_out_at'])

    # 메일 알림 전송
    email = db_connector_email(u_id)
    print(email)
    msg = Message('🚨침입감지🚨', sender='moondansok66@gmail.com', recipients=email)
    msg.body = '침입이 감지되었습니다.'
    mail.send(msg)
        
    return jsonify([lst])

if __name__=='__main__':
    app.run(host='127.0.0.1',port=9000)

=> Flask에 CORS를 import하여 CORS(app) 코드만 추가하면 CORS 충돌 해결!

 

해결방법 블로그)

https://sup-report.tistory.com/74

 

 

 

 

댓글