Sign in
Log inSign up

Making calls to my Flask API returns CORS issues

mark barton's photo
mark barton
·Apr 3, 2019

I have a Flask API set up on a linode server. Using Nginx and Gunicorn, I now forward connections from the public IP to a Flask API.py file.

Running on a server using simpleHTTPServer, I am attempting to make GET Requests to the Flask API using AJAX along with the route URI. Testing the URI and its argument returns the expected result when I type it into my web browser, but any attempted connections made by the simpleHTTPServer result in a CORS error

Access to XMLHttpRequest at '176.58.103.74/api/v1/proxy?address=www.goo… from origin 'localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Any Ideas how to fix? Heres my Flask and AJAX Code.

from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

@app.route('/api/v1/proxy', methods=['GET'])
def api_id():

    headers = {'User-Agent': 'MonkeyPuzzle_Web_v1'}

    address = request.args.get('address')
    new_address = address.replace('www.', 'https://', 1)
    resp = requests.get(new_address, headers=headers)

    return jsonify(result=resp.text)
$.ajax({
        type: 'GET',
        url: '176.58.103.74/api/v1/proxy',   //this will work if the napier server can be used
        data:{'address' : title},
        success: function(data) {
            $("#webIframe_" + tab_id).attr('srcdoc', data.result); 
            console.log(data);
        }
    });