情况:这是我的第一个全栈项目,我使用 HTML/CSS/Bootstrap 和 Python/Flask/SQLite 来构建一个简单的租赁预订 Web 应用程序;主页已完成,其中包括一个简单的联系表格,供访客发送有关租赁单位的问题。
我能够创建一个 Flask 服务器,在收到 Curl 请求后,在 SQLite 中存储一条联系消息。该记录包括姓名、电子邮件和联系信息。
问题:当我尝试从 UI 发送相同内容时,我在 Chrome 网络选项卡上收到以下错误代码:“ERR_HTTP_RESPONSE_CODE_FAILURE”
注意:Curl 命令使用 POST,Flask 服务器需要 POST,Web 应用程序联系表单使用 POST。当我尝试这个时,Flask 服务器正在运行。
请让我知道我可以阅读什么或我可以进一步调试的任何可能的地方。
代码片段:
<!-- Contact us starts -->
<div class="contact-form">
<div class="form-header">
<p>Have questions? We'd love to hear from you! Please use this form to get in touch.</p>
</div>
<form name="contact" id="contact-form" method="POST" action="/backend/contact_messages_post_api" data-netlify="true">
<div class="form-group">
<label for="contact_name">Name:</label>
<input type="text" id="contact_name" name="contact_name" required>
</div>
<div class="form-group">
<label for="contact_email">Email:</label>
<input type="email" id="contact_email" name="contact_email" required>
</div>
<div class="form-group">
<label for="contact_message">Message:</label>
<textarea id="contact_message" name="contact_message" required></textarea>
</div>
<div class="call-to-action-container centered">
<input type="submit" value="Send">
</div>
</div>
<!-- Contact us ends -->
import sqlite3
from flask import Flask, request, jsonify
from contact_messages_db import create_contact_table
app = Flask(__name__, static_url_path='/frontend', static_folder='frontend')
# Call the function to create the table
create_contact_table()
@app.route('/backend/contact_messages_post_api', methods=['POST'])
def submit_contact_form():
# Connect to the SQLite database
conn = sqlite3.connect('contact_messages.db')
cursor = conn.cursor()
# Handle the POST request here
data = request.json
# Extract data fields
contact_name = data.get('contact_name')
contact_email = data.get('contact_email')
contact_message = data.get('contact_message')
# Insert data into the database
cursor.execute("INSERT INTO contact_messages (contact_name, contact_email, contact_message) VALUES (?, ?, ?)",
(contact_name, contact_email, contact_message))
# Commit changes
conn.commit()
return jsonify({'message': 'Data received and stored successfully'})
if __name__ == '__main__':
app.run(debug=True)
curl -X POST -H "Content-Type: application/json" -d '{"contact_name": "John Doe", "contact_email": "[email protected]", "contact_message": "Hello, this is test 3."}' http://127.0.0.1:5000/backend/contact_messages_post_api
根/
--后端/
---- contact_messages_post_api.py
---- contact_messages.db
--前端/
----index.html
---- 样式.css
提前致谢。
添加了日志记录,但也没有任何结果。
后来通过在 Chrome 控制台中按错误代码发现了网络问题,我看到标头将转到 127.0.0.1:5500/backend/contact_messages_post_api 当查看终端时,我看到 Flask 服务器正在 127.0.0.1 上运行:5000
调整了表单上的操作属性以定位实际的http端口:action =“127.0.0.1:5000/backend/contact_messages_post_api”
我知道它有效,因为我从我建议的日志记录中收到了错误日志! :) 非常感谢!现在要调试新错误:)