何为跨域跨域
出于浏览器的同源策略限制。同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策略的一种实现。同源策略会阻止一个域的javascript脚本和另外一个域的内容进行交互。所谓同源(即指在同一个域)就是两个页面具有相同的协议(protocol),主机(host)和端口号(port)
链接:https://www.jianshu.com/p/3a9cf8aa6aa9
解决跨域问题
前端登录页面使用post请求无法获取后端数据,后端响应为200,但是前端出现error的请求错误,具体:DOMException: Failed to execute ‘send’ on ‘XMLHttpRequest’: Failed to load 'http://127.0.0.1:5000/db 等等。。
- 在python终端控制台进行第三方包的下载
pip3 install falsk_cors
- 引入允许跨域的相关代码
@app.route('/db_login', methods=['POST'])
@cross_origin(supports_credentials=True)
def db_login():
"""
post请求获取用户账号密码,进行数据库比较
:return: 请求状态码
"""
if request.method == "POST":
_tem_res = request.get_json()
print(_tem_res)
if _tem_res:
if CONST_NAME in _tem_res and CONST_PASS in _tem_res:
_tem_user_name = _tem_res[CONST_NAME]
_tem_user_pass = _tem_res[CONST_PASS]
fin_pass = SM4.encryptSM4(_tem_user_pass)
fin_name = _tem_user_name
return jsonify(SQL.user_login(fin_name, fin_pass))
else:
return jsonify(errorEml())
else:
return jsonify(errorEml())
else:
return jsonify(errorRes())
- 前端成功获取后端数据