[英]Can't use MediaElementAudioSource due to CORS access restrictions
[英]Can't access Flask API on Heroku Due to CORS Issue
我正試圖打電話給我的燒瓶api(在Heroku上托管)。 當我將url放在url欄中時它會起作用,但是當我通過jquery實現它時它就失敗了。
我繼續安裝燒瓶,並添加以下內容:
app = Flask(__name__)
cors = CORS(app)
@app.route("/")
@cross_origin()
def home():
return jsonify({"results":{}})
我還有別的辦法嗎? 我還在我的請求中做了以下事項:
function fetchResults(requestURL, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
callback(xmlHttp.responseText);
} else {
console.error("Error: " + xmlHttp.status + xmlHttp.statusText);
}
}
}
//Make Async GET Request
xmlHttp.open("GET", requestURL, true);
xmlHttp.setRequestHeader('Access-Control-Allow-Origin', '*');
xmlHttp.send(null);
}
Access-Control-Allow-Origin
是您應在響應中設置的標頭而不是請求。 而且cross_origin
為你做到了。
如果你想使用JQuery,只需使用:
$.ajax({
url: ...,
....,
crossDomain: true
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.