flask跨域
from flask import Flask from flask_cors import CORS, cross_origin app = Flask(__name__) CORS(app) @app.route("/") def helloWorld(): return "Hello, cross-origin-world!"
from functools import wraps from flask import make_response 一: @app.route('/result', methods=['GET', 'POST']) def index(): file_object = open('/home/pyspark_project/pyspark_output_result.json') try: result_text = file_object.read() finally: file_object.close() # rst = make_response(result_text) rst.headers['Access-Control-Allow-Origin'] = '*' return rst, 201
from flask import Flask, request import json app = Flask(__name__) @app.route('/') def func(): res = { 'data': { 'data1': [], 'data2': [] } } callback = request.values.get('callback') return ''.join([ callback, '(', json.dumps(res), ');' ]) if __name__ == '__main__': cfg = { 'host': '0.0.0.0', 'port': 1234, 'debug': True } ''' // javascript code: $.ajax({ url: "http://localhost:1234/", dataType: "jsonp", data: {}, success: function(res) { console.log(res); } });''' app.run(**cfg)