今天在用flask写接口的时候,将接口接入网页的时候浏览器控制台报错:
OPTIONS https://tturl.lowion.cn/config=reload
XMLHttpRequest cannot load https://tturl.lowion.cn/config=reload
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
这种报错都是CORS跨域报错,如果你的服务器建在虚拟主机上,有可能无法在nginx里增加允许跨域,这时候就可以在flask里添加“Access-Control-Allow-Origin”请求头允许跨域。
1.代码
def send(msg:eval): backjson = msg fresponse = make_response(backjson) fresponse.headers['Access-Control-Allow-Origin'] = '*' return fresponse
定义的这个方法就是将以字典类型传入的数据打包成一个flask请求并添加一个头
2.使用方法

在方法返回数据的时候使用
return send({你的字典返回})
就可以了
Comments NOTHING