跨域是指从一个域名的网页去请求另一个域名的资源。比如从http://www.baidu.com/ 页面去请求 http://www.google.com 的资源。跨域的严格一点的定义是:只要 协议,域名,端口有任何一个的不同,就被当作是跨域。 如何实现跨域取到数据,LS说的JSONP是最为常见,你可以参考这篇文章 跨域与跨域访问
安装Flask-Cors
pip install Flask-Cors
或https://pypi.python.org/pypi/… 下载tar 解压安装
在文件中加入:
#cors 跨域访问
from flask_cors import *
app = Flask(name);
CORS(app, supports_credentials=True);
即可完成
# -*- coding: utf-8 -*-
from flask import Flask
from flask_restful import reqparse, Api, Resource
from flask_cors import *
from model.ernie_gram_zh_static._36Kr.static_json import PredictErnie
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
app.config["RESTFUL_JSON"] = {'ensure_ascii': False}
CORS(app, supports_credentials=True)
api = Api(app)
Todos = {
'XX': '实体抽取',
}
parser = reqparse.RequestParser()
parser.add_argument('text')
# Todo
# shows a single todo item and lets you delete a todo item
predict_ernie = PredictErnie()
class EntityRec(Resource):
"""
pass
"""
def get(self):
return {'task': Todos.get('XX')}
def post(self):
args = parser.parse_args()
results = predict_ernie.do_predict(args['text'])
return results, 200
# Actually setup the Api resource routing here
api.add_resource(EntityRec, 'XXX')
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=9535)
# server = pywsgi.WSGIServer(('0.0.0.0', 5000), app, log=LogWrite()) # 日志模块影响通信效率,在nginx端做
print('server 已启动')
# server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
# server.serve_forever()