简体   繁体   中英

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在此处输入图像描述

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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