【发布时间】:2017-04-19 18:22:13
【问题描述】:
我遇到了一些关于“跨源请求被阻止”的问题。我试图首先允许来自服务器,而不是来自所有(“*”)。每次在 chrome 开发人员工具包上都会收到相同的错误消息。
这是我的烧瓶 python 代码:
application = Flask(__name__)
application.config.from_object(__name__)
cors = CORS(application, resorces={r'/*': {"origins": '*'}})
@application.route("/get-live-data",methods=['GET'])
@cross_origin()
def live_data():
con = connect_db()
cur = con.cursor()
cur.execute("SELECT * from envoiContinuT")
sqlite_result = cur.fetchall()
cle = json.load(open(JSON_STATUS))
parametres = json.load(open(JSON_PARAMETRES))
descT = []
for key in cle["status"]:
attr = parametres[key]
if attr["envoiC"] == 1:
descT.append(attr["description"])
response = any_response(flask.jsonify(data=descT))
return response
这是我的 Ajax 代码:
var baseURL = "http://localhost:8000";
function getLiveData(data){
//Get the parameters descriptions
$.ajax({
method: 'GET',
url:baseURL + '/get-live-data',
headers: {
"Accept" : "application/json",
"Content-type": "application/json"
},
success:function(data){
console.log(data);
//populateAccordion(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("Status: " + textStatus);
console.log("Error: " + errorThrown);
}
});
}
感谢您的回答!
【问题讨论】:
标签: javascript python ajax flask cors