In making my flask server, I am getting a not found error when calling my flask backend.
index.html:
<!DOCTYPE html>
Button Demo
Click me!
document.getElementById('myButton').addEventListener('click', function() {
fetch('/get_hello') // Use a relative URL
.then(response =\> response.text())
.then(data =\> alert(data))
.catch(error =\> console.error('Error:', error));
});
app.py:
from flask import Flask
from flask_cors import CORS # Import CORS from flask_cors
app = Flask`__name__)
CORS(app) # Enable CORS for your Flask app
@app.route('/')
def index():
return open('index.html').read()
@app.route('/get_hello')
def get_hello():
return 'hello world'
if __name__ == '__main__':
app.run(debug=True, port=8000)
Instead of alerting "hello world" I have been receiving an html response claiming error 404.
Both of these are being hosted on port 8000.
I tried using CORS and changing the port of the flask server; however, that caused more problems than it fixed.
I also tried different browsers (safari and duckduckgo) to test whether or not the browser was causing the issue.
Using '/get_hello' and setting up CORS implies you are serving the front-end on a different host. Something like this is what you would help.
templates
folder and add your html file call itindex.html