繁体   English   中英

如何允许 CORS 与 Flask 获得对预检请求的响应没有 http 正常状态

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM