简体   繁体   English

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

[英]How to allow CORS with Flask getting Response to preflight request does not have http ok status

I'm trying to have my client-side Javascript code send post requests to my backend in Flask and I've used this answer issue with flask-cors - blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status but I still keep on getting this error我正在尝试让我的客户端 Javascript 代码在 Flask 中将发布请求发送到我的后端,并且我已经将这个答案问题与 flask-cors 一起使用 - 被 Z5A8FEFF0B4BDE3EEC9244B7B7A8FEFF0B4BDE3EEC9244B776023B791DZ 阻止访问控制检查响应:未通过预检请求它没有 HTTP ok 状态,但我仍然不断收到此错误在此处输入图像描述

Here is my client side code这是我的客户端代码

axios
            .post(`http://127.0.0.1:5000/tags/`, {
              image: srcUrl
            })
            .then(function(response) {
              console.log(response);
            })
            .catch(function(error) {
              console.log(error);
            });

Here is my backend这是我的后端

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}

Try to add the origin resources in the CORS declaration in your application,尝试在您的应用程序的 CORS 声明中添加源资源,

app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route("/api/v1/users")

For more references, you can visit https://github.com/corydolphin/flask-cors/blob/master/README.rst更多参考,可以访问https://github.com/corydolphin/flask-cors/blob/master/README.rst

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态。 REACTJS, AXIOS - CORS: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. REACTJS, AXIOS 预检响应在axios中没有HTTP正常状态 - Response for preflight does not have HTTP ok status in axios MVC Azure + AjaxCall + microsofttranslator +对预检请求的响应未通过访问控制检查:它没有HTTP ok状态 - MVC Azure + AjaxCall + microsofttranslator + Response to preflight request doesn't pass access control check: It does not have HTTP ok status CORS 错误:它没有 HTTP 正常状态 - CORS Error: It does not have HTTP ok status 预检请求未通过访问控制检查:它没有 HTTP ok 状态 - Preflight request doesn't pass access control check: It does not have HTTP ok status Cors 策略错误:它没有 HTTP 正常状态 - Cors Policity Error: It does not have HTTP ok status 如何在Socket.io中修复cors策略问题“它没有HTTP ok状态” - How to fix cors policy problem “It does not have HTTP ok status” in Socket.io 从本地 .html 文件向 localhost:8080 发送 AJAX POST 请求时出错。 CORS:“它没有 HTTP ok 状态。” - Error sending AJAX POST request from local .html file to localhost:8080. CORS: "It does not have HTTP ok status." CORS问题? -预检响应具有无效的HTTP状态代码405 - CORS issue? - Response for preflight has invalid HTTP status code 405 响应预检请求的 Cors 问题 - Cors issue with response to preflight request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM