Flask 405-Methode für POST-Anfrage von PyScript nicht zulässigPython

Python-Programme
Guest
 Flask 405-Methode für POST-Anfrage von PyScript nicht zulässig

Post by Guest »

Ich arbeite an einem Flask-Projekt, bei dem ich Daten von einem PyScript (das im Browser läuft) mithilfe einer POST-Anfrage an das Flask-Backend senden muss. Wenn ich jedoch versuche, eine POST-Anfrage vom PyScript-Frontend an das Flask-Backend zu stellen, erhalte ich die Fehlermeldung:

405 Method Not Allowed

Flask-Backend

Code: Select all

from flask import Flask, render_template, request, jsonify
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/data/submit', methods=['POST'])
def handle_data():
data = request.get_json()  # Get JSON data from the request body
print(f"Received data: {data}")
response = {"status": "success", "received": data}
return jsonify(response)

if __name__ == '__main__':
app.run(debug=True)
PyScript-Frontend:

Code: Select all





PyScript 与后端交互






PyScript 与后端交互示例

import js

def send_data_to_backend():
data = {"message": "Hello from PyScript"}
js.fetch('/data/submit', {
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
},
'body': js.JSON.stringify(data)
}).then(lambda response: response.json()).then(lambda data: js.console.log(data))

send_data_to_backend()




Problem

Code: Select all

Request URL: http://127.0.0.1:5000/data/submit
Status Code: 405 METHOD NOT ALLOWED

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post