網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、Post
通式
axios.post('api地址',待傳入的參數(shù))
.then(function (response) {
//handle success data
})
.catch(function (error) {
//handle error satuation
console.log(error)
})
}
應(yīng)用
第二個(gè)參數(shù)第一種寫(xiě)法:直接傳字符串
const posts_ = 'https://www.fastmock.site/mock/4f2f543c48ec3d334a328af2e83239e6/MockData/api/postDemo'
const axios = require('axios');
// 第一個(gè)參數(shù)是post接口地址,第二個(gè)參數(shù)是往post寫(xiě)入數(shù)據(jù),這里是獲取表單的值
axios.post(posts_, PostVal)
.then(function (response) {
//response.config.data 獲取表單值的地方
console.log(response.config.data)
})
.catch(function (error) {
console.log(error)
})
第二個(gè)參數(shù)的另一種寫(xiě)法:傳json格式的對(duì)象
const axios = require('axios');
axios.post('https://www.fastmock.site/mock/4f2f543c48ec3d334a328af2e83239e6/MockData/api/postDemo', {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"PostVal": PostVal,
},
"id": 1,
"auth": null
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
二、Get
通式
axios.get('api地址')
.then(function (response) {
//handle success data
})
.catch(function (error) {
//handle error satuation
console.log(error)
})
}
應(yīng)用
const getApi = () => {
axios.get(api)
.then(function (response) {
//handle success data
const result = response.data.data
setUserName(result.userName);
setEmail(result.email);
setUserId(result.userId);
setMsg(response.data.msg);
})
.catch(function (error) {
//handle error satuation
console.log(error)
})
}
三、報(bào)錯(cuò)提示:console.log(response)返回[object,object]
- 用
JSON.stringfy(response)
將response對(duì)象轉(zhuǎn)為字符串,但是不好獲取里面的屬性;
希望有其他更好的解決辦法
參考文章
axios 獲取post數(shù)據(jù)
四、完整代碼
import React, { useRef, useState } from 'react'
import axios from 'axios'
import styles from "./index.less"
export default () => {
const [userName, setUserName] = useState()
const [email, setEmail] = useState()
const [userId, setUserId] = useState()
const [msg, setMsg] = useState()
const api = 'https://www.fastmock.site/mock/4f2f543c48ec3d334a328af2e83239e6/MockData/api/mock'
const getApi = () => {
axios.get(api)
.then(function (response) {
//handle success data
const result = response.data.data
setUserName(result.userName);
setEmail(result.email);
setUserId(result.userId);
setMsg(response.data.msg);
})
.catch(function (error) {
//handle error satuation
console.log(error)
})
}
const [PostVal, setPostValue] = useState();
const postContent = useRef(null);
const posts_ = 'https://www.fastmock.site/mock/4f2f543c48ec3d334a328af2e83239e6/MockData/api/postDemo'
function postApi() {
if (postContent.current.value.trim() !== '') {
//the second program uses to get post content
axios.post(posts_, PostVal)
.then(function (response) {
//response.config.data 獲取表單值的地方
console.log(response.config.data)
})
.catch(function (error) {
console.log(error)
})
}
else {
alert('please input something!')
}
}
function handleSubmit() {
if (postContent.current.value.trim() !== '') {
return setPostValue(postContent.current.value.trim());
}
alert('please input something!')
}
return (
<div className={styles.InterFace}>
<h3>Get 請(qǐng)求獲取接口數(shù)據(jù)</h3>
<button type="button" onClick={getApi}>Get api</button>
<div>
<p> <b>userName: </b>{userName}</p>
<p><b>userId: </b>{userId}</p>
<p><b>email: </b>{email}</p>
<p><b>msg: </b>{msg}</p>
</div>
<h3>Post 請(qǐng)求獲取接口數(shù)據(jù)</h3>
<div>
<input type="text" ref={postContent} />
<p><b>post value: </b>{PostVal}</p>
<button type="button" onClick={handleSubmit}>Submit</button>
<button type="button" onClick={postApi}>Post api</button>
</div>
</div>
)
}
五、自造測(cè)試接口
fastmock
使用步驟參考官方文檔教程
原文鏈接:https://blog.csdn.net/weixin_46353030/article/details/123458121
相關(guān)推薦
- 2024-04-01 使用Vite安裝TailwindCSS
- 2022-05-31 利用Python進(jìn)行數(shù)據(jù)清洗的操作指南_python
- 2023-01-20 .Net執(zhí)行SQL存儲(chǔ)過(guò)程之易用輕量工具詳解_ASP.NET
- 2022-03-26 postman模擬post請(qǐng)求的四種請(qǐng)求體_相關(guān)技巧
- 2022-09-19 Tomcat10配置端口號(hào)為443(使用https訪問(wèn))_Tomcat
- 2022-07-17 關(guān)于elasticsearch連接時(shí)斷時(shí)續(xù)以及Kibana出現(xiàn)server is not ready
- 2022-07-16 windows下 使用 nvm-windows 管理node版本
- 2023-04-26 C語(yǔ)言二維數(shù)組指針的概念及使用_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支