On Pythonanywhere I created a Flask app, to use it as a RESTFul API. My frontend code is hosted on GitHub Pages and it always says that the answer has a CORS Error. From other source I can easily use it without any error. How is it possible?
Here is my python code:
from flask import Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "https://username.github.io"}})
@app.route('/api/datagetter')
def get_data():
my_list = [
{
'id': 1,
'userid': 0,
'amount': 3
}
]
return jsonify({'list': my_list})
if __name__ == '__main__':
app.run(debug=True)
And this is the JavaScript:
function fetchData() {
fetch('http://username.pythonanywhere.com/api/datagetter')
.then(response => response.json())
.then(data => {
console.log('Fetched data:', data);
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
As I mentioned if I call the url from the browser, I get the answer, the error is only comes up from the GitHub Pages website. (http://username.pythonanywhere.com/api/datagetter)