網(wǎng)站首頁 編程語言 正文
react axios結(jié)合后端實現(xiàn)GET和POST請求
區(qū)別在這里不做介紹了,POST方法比GET方法稍微安全一點,GET方法比POST方法要快一些,個人感覺傳遞單個參數(shù)用GET,傳遞多個參數(shù)用POST。
get實現(xiàn)方式1(參數(shù)直接在url中)
- 前端
export function getAllSubstationsByUser() {
? return axios.get(`/api/integratedEnergy/all/${user}/substations`);
}
- 后端
? ?@RequestMapping(value = "/all/{user}/all/substations", method = RequestMethod.GET)
? ? public ?ResponseEntity<List<Map<String, Object>>> getAllSubstationsByUserAreas(@PathVariable("user") String user) {
? ? String a = user;
? ? // todo 實現(xiàn)方法
}
get時間方式2(作為JSONString跟在url末尾)
- 前端
? const params = {
? ? ? user: 'admin',
? ? };
? ??
export function getAllSubstationsByUser(params) {
? return axios.get(`/api/integratedEnergy/all/substations`, { params });
}
- 后端
? ? @RequestMapping(value = "/all/substations", method = RequestMethod.GET)
? ? public ResponseEntity<List<Map<String, Object>>> getAllSubstationsByUserAreas(@RequestParam(value = "user", defaultValue = "") String user) {
? ? ? ? List<Map<String, Object>> mapList = new ArrayList<>();
? ? ? ? String a = user;
? ? ? ? // todo 實現(xiàn)方法
? ? ? ? return new ResponseEntity<>(mapList, HttpStatus.OK);
? ? }
post實現(xiàn)(傳遞JSONObject)
- 前端
const params = { id: 'admin', name: '用戶' }
export function getChildrenDevicesByParent(params) {
? return axios.post(`/api/integratedEnergy/all/child/devices`, params);
}
- 后端
? ?@RequestMapping(value = "/all/child/devices", method = RequestMethod.POST)
? ? public ResponseEntity<List<Map<String, Object>>> getStorageHistoryData(@RequestBody JSONObject params) {
? ? List<Map<String, Object>> mapList = new ArrayList<>();
?? ?String id = params.getString("id").trim());
?? ?String name = params.getString("name").trim());
?? ?// todo 實現(xiàn)方法
? ? return new ResponseEntity<>(mapList, HttpStatus.OK);
? ? }
react?項目axios請求配置
axios請求封裝
1、安裝 npm I axios
2、首先在根目錄下建立server.js文件內(nèi)容如下
import axios from 'axios'
axios.defaults.baseURL = '' ?//根據(jù)項目自己更改
//一些配置,發(fā)起請求和響應(yīng)可以打印出來查看
axios.interceptors.request.use((config)=>{
? ? //這里是用戶登錄的時候,將token寫入了sessionStorage中了,之后進行其他的接口操作時,進行身份驗證。
? ? config.headers.token = localStorage.getItem("cookie");
? ? return config;
})
//在response中
axios.interceptors.response.use(config =>{
? ? return config;
})
const http = {
? ? post:'',
? ? get:'',
? ? getParams:''
}
http.post = function (api, data){ ?// post請求
? ? return new Promise((resolve, reject)=>{
? ? ? ? axios.post(api,data).then(response=>{
? ? ? ? ? ? resolve(response)
? ? ? ? })
? ? })
}
http.get = function (api, data){ // get請求
? ? return new Promise((resolve, reject)=>{
? ? ? ? axios.get(api,data).then(response=>{
? ? ? ? ? ? resolve(response)
? ? ? ? })
? ? })
}
http.getParams = function (api, param){ //get請求 需要傳參
? ? return new Promise((resolve, reject)=>{
? ? ? ? axios.get(api, {params: param}).then(response => {
? ? ? ? ? ? resolve(response.data)
? ? ? ? }, err => {
? ? ? ? ? ? reject(err)
? ? ? ? }).catch((error) => {
? ? ? ? ? ? reject(error)
? ? ? ? })
? ? })
}
export default http
3、組件中使用
import http from '../../server'; ?// 首先引入server文件
http.get('api/接口名稱').then(res => {
? ? ? ?console.log(res)
}).catch(error => {
? ? ? ?console.error(error)
})
這個時候請求接口我們回遇到跨域的問題,接下來告訴你們?nèi)绾谓鉀Q跨域
1、在根目錄下建立setupProxy.js文件內(nèi)容如下
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
? app.use(
? ? '/api',
? ? proxy.createProxyMiddleware({
? ? ? target: 'http://172.21.211.132:8000', // 后臺服務(wù)地址以及端口號
? ? ? changeOrigin: true, // 是否跨域
? ? ? pathRewrite: {
? ? ? ? '^/api': '' // 路徑重寫,用/api代替target里的地址
? ? ? }
? ? })
? );
};
總結(jié)
原文鏈接:https://blog.csdn.net/qq_34307801/article/details/105046975
相關(guān)推薦
- 2023-10-17 css標(biāo)簽畫圓形進度條
- 2022-07-14 Python內(nèi)建類型list源碼學(xué)習(xí)_python
- 2022-07-02 教你使用Pycharm配置遠程Jupyter_python
- 2023-04-16 c#?成員類型訪問權(quán)限低于字段本身的實現(xiàn)_C#教程
- 2022-10-28 Go語言包和包管理詳解_Golang
- 2022-06-09 FreeRTOS實時操作系統(tǒng)的內(nèi)核控制示例解析_操作系統(tǒng)
- 2023-02-01 C++泛型編程綜合講解_C 語言
- 2022-08-06 Golang使用Consul詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支