【发布时间】:2015-05-08 19:10:52
【问题描述】:
我有一个烧瓶应用程序,其中有一个我试图通过远程服务器调用的 restful api。
初始化文件:-
from flask.ext import restful
from flask.ext.cors import CORS
app = create_app(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resorces={r'/d/<string:d_name>': {"origins": '*'}})
api=restful.Api(app)
api.add_resource(g_Api, '/g/<string:g_id>')
api.add_resource(d_Api, '/d/<string:d_name>')
现在是 d_Api 类:-
from flask import Flask, render_template, g
from flask.ext.restful import reqparse, abort, Api, Resource
def abort_if_not_exist(d_name):
return d_name
class d_Api(Resource):
def __init__(self):
self.d=d
def get(self, d_name):
val=abort_if_not_exist(d_name)
return val
这在同一个本地主机服务器上工作正常,返回正确的结果。 localhost 上运行的服务器对 api 进行了 ajax 调用,
$.ajax({
async: false,
type: 'GET',
url: 'http://localhost:8080/d/'+d_name,
success: function(data) {
alert(data);
}
});
当从 remotehost 调用时不返回任何响应,而是在 Firefox 中我得到 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource d 。这可以通过将资源移动到同一域或启用 CORS 来解决。
Im not sure how else to configure CORS for this api endpoint. Im 使用 python 2.6 和“flask-cors”。
我发现了这个区别:当我尝试从本地主机访问 api 时 - 2015-03-09 11:40:35 - Flask-Cors:385 - INFO - CORS request from Origin:xyz-ld2.abc.biz:8080, setting Access-Control-Allow-Origin:*
当我尝试从远程主机访问 api 时:2015-03-09 11:47:15 - Flask-Cors:385 - INFO - CORS request from Origin:None, setting Access-Control-Allow-Origin:*
【问题讨论】:
-
你没有拼错:“resorces”。至少 3.0.2 拼写为“资源”:flask-cors.readthedocs.io/en/3.0.2
标签: python ajax flask cors flask-restful