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)
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()
Code: Select all
Request URL: http://127.0.0.1:5000/data/submit
Status Code: 405 METHOD NOT ALLOWED