如何让flask-cor在Google App Engine上返回Access-Control-Allow-Origin?

时间:2015-06-10 19:00:59

标签: python google-app-engine flask

我的python应用程序不会在Google App Engine上引发任何错误,但永远不会发送Allow-Control-Access-Origin标头。我怎样才能确保我用烧瓶发送它?

import MySQLdb
import os
import webapp2
import json
import flask
from flask import Flask, jsonify, render_template, request
from flask.ext.cors import CORS, cross_origin

app = Flask(__name__)

@app.route('/')
@cross_origin()    
def do_search():
  if (os.getenv('SERVER_SOFTWARE') and
    os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
    db = MySQLdb.connect(unix_socket='/cloudsql/my-instance-name:storeaddressdb', db='store_locator', user='myuser', passwd='password')
  else:
    db = MySQLdb.connect(host='localhost', user='root')

  cursor = db.cursor()

  query = 'SELECT * FROM stores WHERE 1 LIMIT 5'
  cursor.execute(query)
  resp = jsonify(data=cursor.fetchall())
  return resp

  db.close()

1 个答案:

答案 0 :(得分:0)

看来这段代码确实有效,但是当我使用cURL来测试url时,我看不到Allow-Control-Access-Origin标头。 Google App Engine中的日志显示CORS有选择地添加标题,除非客户明确跳过它。此外,Javascript客户端不再显示错误,并接收数据。它可能不是最漂亮的python代码,但它在功能上符合我的目的。