I’m currently developing a basic web platform using javascript, html, and python flask. I’m using firebase for hosting. I have a javascript function looking something like this:
function giveData() { const data = { variable1: testName, variable2: timeUntil, variable3: timeCommit, variable4: additionalInfo }; console.log("Sending data to server:", data); fetch('/mywebsite/update_data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) }
and have some more code structured to receive and display the response. In my python, I have something like this:
`@app.route(‘/mywebsite/update_data’, methods=[‘POST’])
@cross_origin()
def update_data():
global actualData # Declare actualData as global
actualData = request.json
topics_list = getTopics(actualData['variable1'], actualData['variable2'], actualData['variable3'], actualData['variable4'])
return jsonify({'topics_list': topics_list})`
where “mywebsite” is the project’s website I have it hosted on firebase. However, when I try to run the code, the giveData() function returns the following errors:
“Failed to load resource: the server responded with a status of 404 ()” from “/mywebsite/update_data:1”, and
“SyntaxError: Unexpected token ‘<‘, “<!DOCTYPE “… is not valid JSON” from script.js/144.
I think the two errors are really the same problem, which is that the fetch command can’t find the file and function from python. I have all the files in my public directory, which is the one I specified for hosting on Firebase. I’ve tried a lot of different paths in the fetch statement, but I can’t seem to get anything to work. Also, for some reason, my partner ran it from VSCODE locally using the port 127.0.0.1:5000, and it worked there. It seems to be some issue on my end, or with the hosting.
Any help would be greatly appreciated. Thank you so much in advance.
user25440564 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.