網(wǎng)站首頁 編程語言 正文
Axios 是一個基于promise的HTTP庫,該庫是一個更好的替代ajax向后端發(fā)送數(shù)據(jù)或請求數(shù)據(jù)的前端組件庫,其本質上也是對原生XHR的封裝,只不過它是Promise的實現(xiàn)版本,符合最新的ES規(guī)范,如下案例運用axios向后端提交JSON字符串,后端通過Flask響應請求并處理。
前端運用Axios發(fā)送數(shù)據(jù)的兩種方式。
<html>
<head>
<meta charset="UTF-8">
<title>LyShark</title>
<script src="https://cdn.lyshark.com/javascript/axios/0.26.0/axios.min.js"></script>
</head>
<body>
<input type="text" name="name" id="name" />
<input type="text" name="age" id="age" />
<button onclick="saveHanderPost()" >提交</button>
</body>
<!-- 第一種發(fā)送方法 -->
<script type="text/javascript">
function saveHanderPost()
{
let name = document.getElementById("name").value;
let age = document.getElementById("age").value;
axios.post("/",{
name:name,
age:age
})
.then(function(response){
console.log(response);
console.log(response.data.username);
console.log(response.data.message);
})
.catch(function(error){
console.log(error);
})
}
</script>
<!-- 第二種發(fā)送方法 -->
<script type="text/javascript">
function saveHanderPostB()
{
let name = document.getElementById("name").value;
let age = document.getElementById("age").value;
axios({
url: "/",
method: "post",
data: {
name: name,
age:age
},
responseType: "text/json",
})
.then(function(response){
console.log(response);
console.log(response.data.username);
console.log(response.data.message);
})
.catch(function(error){
console.log(error);
})
}
</script>
</html>
Python后端使用Flask接收并處理前端發(fā)送過來的JSON字符串。
from flask import Flask,render_template,request
import json
app = Flask(import_name=__name__,
static_url_path='/python', # 配置靜態(tài)文件的訪問url前綴
static_folder='static', # 配置靜態(tài)文件的文件夾
template_folder='templates') # 配置模板文件的文件夾
@app.route('/', methods=["GET","POST"])
def index():
if request.method == "GET":
return render_template("index.html")
elif request.method == "POST":
val = request.get_json()
print("收到用戶: {} ---> 年齡: {}".format(val["name"],val["age"]))
# 返回JSON類型
return json.dumps({"username": "lyshark","message": "hello lyshark"})
if __name__ == '__main__':
app.run(host="127.0.0.1", port=80, debug=False)
運行后提交數(shù)據(jù)前后端均可接收到數(shù)據(jù):
原文鏈接:https://juejin.cn/post/7179544803539943485
相關推薦
- 2022-05-01 Windows系統(tǒng)安裝redis數(shù)據(jù)庫_Redis
- 2022-12-11 C語言如何計算兩個數(shù)的最小公倍數(shù)_C 語言
- 2022-10-24 C++??STL?_?Vector使用及模擬實現(xiàn)_C 語言
- 2022-11-06 python使用minimize()?函數(shù)替代matlab的fmincon函數(shù)_python
- 2022-06-21 .NET?Core?API之格式化輸出對象OutputFormatter_實用技巧
- 2022-04-23 通過自定義指令實現(xiàn) element-ui的tooltip組件 文本長度超出顯示不超出不顯示
- 2022-02-11 element ui table 內嵌 input 調用 focus 方法無效
- 2022-02-05 Numpy中不同維度數(shù)組之間的計算
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支