實作效果:

點擊提交后:


前端頁面原始碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ajax前后端互動_通過name屬性進行</title>
</head>
<body>
<form action="/" method="post">
用戶名:<input type="text" placeholder="請輸入用戶名" name="user"><br>
密 碼:<input type="password" placeholder="請輸入密碼" name="pwd"><br>
性別:<label for="male">男</label>
<input type="radio" name="sex" value="male" id="male">
<label for="female">女</label>
<input type="radio" name="sex" value="female" id="female">
<label for="no">保密</label>
<input type="radio" name="sex" value="no" id="no" checked>
<br>
愛好:<label for="sing">唱歌</label>
<input type="checkbox" name="hobby" id="sing" value="sing" checked>
<label for="dance">跳舞</label>
<input type="checkbox" name="hobby" id="dance" value="dance">
<label for="draw">畫畫</label>
<input type="checkbox" name="hobby" id="draw" value="draw">
<br>
上傳頭像:<input type="file" accept="image/*" name="file"><br>
地址:<select name="address" id="address" size="1">
<optgroup label="河南省">
<option value="ZZ">鄭州市</option>
<option value="XY">信陽市</option>
<option value="NY">南陽市</option>
</optgroup>
<optgroup label="湖北省">
<option value="WH">武漢市</option>
</optgroup>
<optgroup label="湖南市">
<option value="CS">長沙市</option>
</optgroup>
</select>
<br>
個人簡介:<textarea name="introduce" id="introduce" cols="30" rows="10"></textarea><br>
<input type="submit" value="提交">
<input type="reset">
</form>
</body>
</html>
后端服務器原始碼:
import tornado.web
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("Ajax_前端.html")
def post(self, *args, **kwargs):
print(self.get_argument("user"))
print(self.get_argument("pwd"))
print(self.get_argument("sex"))
# 愛好是不定長的,所以用不定長引數arguments
print(self.get_arguments("hobby"))
print(self.get_argument("file"))
print(self.get_argument("address"))
print(self.get_argument("introduce"))
self.write("提交成功")
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler),
])
application.listen(8888)
tornad
小提醒,如果運行報錯,加入以下代碼:
# windows 系統下 tornado 使用 SelectorEventLoop
import platform
if platform.system() == "Windows":
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/357134.html
標籤:其他
