[英]CORS: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. REACTJS, AXIOS
[英]How to allow CORS with Flask getting Response to preflight request does not have http ok status
我正在嘗試讓我的客戶端 Javascript 代碼在 Flask 中將發布請求發送到我的后端,並且我已經將這個答案問題與 flask-cors 一起使用 - 被 Z5A8FEFF0B4BDE3EEC9244B7B7A8FEFF0B4BDE3EEC9244B776023B791DZ 阻止訪問控制檢查響應:未通過預檢請求它沒有 HTTP ok 狀態,但我仍然不斷收到此錯誤
這是我的客戶端代碼
axios
.post(`http://127.0.0.1:5000/tags/`, {
image: srcUrl
})
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
這是我的后端
from imageai.Classification import ImageClassification
import os
from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route('/')
@cross_origin()
def hello_world():
return {"What's": "up"}
@app.route('/tags')
@cross_origin()
def put_tags(image):
execution_path = os.getcwd()
prediction = ImageClassification()
# prediction.setModelTypeAsResNet50()
# prediction.setModelTypeAsInceptionV3()
prediction.setModelTypeAsDenseNet121()
prediction.setModelPath(os.path.join(
execution_path, "DenseNet-BC-121-32.h5"))
prediction.loadModel()
predictions, probabilities = prediction.classifyImage(
os.path.join(execution_path, image), result_count=5)
final = []
for eachPrediction, eachProbability in zip(predictions, probabilities):
final.append(eachPrediction)
return {'tags': final}
嘗試在您的應用程序的 CORS 聲明中添加源資源,
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route("/api/v1/users")
更多參考,可以訪問https://github.com/corydolphin/flask-cors/blob/master/README.rst
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.