Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

javascript - Flask-Cors not working Nuxt + Flask (JWT used)

I have read all available solutions to my problem, still cannot figure out problem.

I have tried all available solutions, but cannot find anything helpful.

Whenever I send request, browser blocks request under CORS Policy.

My Flask App responds to OPTIONS call with code 308.

I have also tried adding resources={r"*": {"origins": "*"}} while initialising object.

I have also tried doing app.config['CORS_HEADERS'] = 'Content-Type'

Nothing Works.

Backend:

from flask import Flask, make_response
from flask_cors import CORS

from tracer.blueprints.auth import app as auth_blueprint
from tracer.blueprints.url import app as url_blueprint

app = Flask(__name__)
CORS(app)

app.register_blueprint(auth_blueprint, url_prefix='/auth')
app.register_blueprint(url_blueprint, url_prefix='/url')

@app.route('/')
def home():
    return make_response('<center><h2>APIs hosted here.</h2></center>')

if __name__ == '__main__':
    app.run()

Frontend: (Nuxt Axios):

  context.$axios.setHeader('token', token);
  context.$axios.setHeader('Content-Type', 'application/json')
  const response = await context.$axios.$post(
    "http://127.0.0.1:5000/url",
    {
      redirect: url
    }
  );
question from:https://stackoverflow.com/questions/65939698/flask-cors-not-working-nuxt-flask-jwt-used

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured out the solution. Thought I should share it.

Problem was, I was using url_prefix in blueprints, which led to problems in flask_cors. I suggest if anyone is having same problem,

remove url_prefix from register_blueprint and manually add url prefix in route definition.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...