0

I am currently trying to do a fetch request when logged into the web app but i keep getting a .

87466804-e84d-413e-abd1-74dc56624149-ide.cs50.xyz/sell:1 Failed to load resource: the server responded with a status of 502 (Bad Gateway)

and then

sell:1 Access to fetch at 'http://87466804-e84d-413e-abd1-74dc56624149-ide.cs50.xyz/sell' from origin 'http://127.0.0.1:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

My Flask app looks like

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    ticker_symbols = helper_index.get_ticker_symbols(currently_owned_table)

    #handles GET request
    if request.method == "GET":
        return render_template("sell.html", companies=ticker_symbols)

    if request.method == "POST":

        #handles submitted form
        if "sell_form" in request.form:
            #if form is submitted


            return "thanks"

        #handles fetch request
        else:
            the_request = request.get_json()
            the_symbol = [the_request["symbol"]]
            shares_available = helper_index.get_total_shares(the_symbol, currently_owned_table)
            requested_shares = the_request["shares"]

            print(requested_shares)

            if int(requested_shares) <= shares_available[0]:
                return "true"
            else:
                return "false"

I'm unsure how to fix this? and ideas appreciated

1 Answers1

0

Flask-CORS does magic.

app.py:

app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})

@app.route("/api/v1/users")
    def list_users():
    return "user example"
Jan
  • 46
  • 5