数据请求(axios)

news/2024/10/19 9:35:01/

首先使用npm安装axios
接着需要使用的组件中引入:
import axios from ‘axios’
最后在methods(){}中使用

GET请求
axios.get(‘/user?ID=12345’)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
// 功能同上
axios.get(‘/user’, {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// 想使用异步/等待吗?将“asycc”关键字添加到外部函数/方法中
async function getUser() {
try {
const response = await axios.get(‘/user?ID=12345’);
console.log(response);
} catch (error) {
console.error(error);
}
}

POST请求
axios.post(‘/user’, {
    firstName: ‘Fred’,
    lastName: ‘Flintstone’
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
完整版:
axios({
url: ‘/user’,
method: ‘post’,
data: {
firstName: ‘Fred’,
lastName: ‘Flintstone’
},
transformRequest: [function (data) {
// Do whatever you want to transform the data
let ret = ‘’
for (let it in data) {
ret += encodeURIComponent(it) + ‘=’ + encodeURIComponent(data[it]) + ‘&’
}
return ret
}],
headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’
}
})

执行多个并发请求
function getUserAccount() {
  return axios.get(‘/user/12345’);
}
 
function getUserPermissions() {
  return axios.get(‘/user/12345/permissions’);
}
 
axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    // Both requests are now complete
  }));

项目中使用:
let json = [],
me = this;
;
json.chinese = this.temp.title
json.english = this.temp.author
json.project = this.currentProject
console.log(json)
axios({
url: ‘http://39.105.105.188:7010/form/{projectName}/addFields’,
method: ‘post’,
data: [
{
“chinese”: json.chinese,
“english”: json.english,
“id”: 0,
“project”: json.project
}
],
headers: {
‘Content-Type’: ‘application/json’, /* json格式数据 */
“Authorization”: 'Bearer ’ + me.$store.state.user.token
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});


http://www.ppmy.cn/news/477671.html

相关文章

软件工程报告

软件工程报告 1. 引言 本报告总结和分析了软件工程项目的开发过程和结果。我们介绍了项目的目标、方法、实施过程和最终成果,同时也探讨了遇到的挑战和取得的成功。 2. 项目概述 本项目的目标是开发一个功能完善的在线购物平台,旨在满足用户的购物需…

VirtualBox修改虚拟机用户密码

1、启动系统,系统会出现引导程序,我们需要在引导页面选择不同的操作,以便让引导程序暂停,出现下方暂停后的界面,可以按 ↑或↓ 选操作,此时,按下键盘的 e,进入编辑模式 2、将光标移动…

CTF-密码学相关

参考:千千秀字、百度百科、CTF编码和加密总结 、CTF常见编码和加密特征 、CTF中Crypty(密码类)入门必看 目录 字符编码 1.ASCII编码 2.Unicode编码 3.UTF-8编码 4.UTF-16编码 5.进制转换 6.URL字符编码 7.摩斯电码 8.Base64/32/16编…

java设计帐号密码_怎样用java设置帐号和密码

展开全部 public class User{ //定义私有属性 用户名和密码 private String userName; private String password; public User(String userName,String password){ this.userNameuserName; this.passwordpassword; } //私有属性的set get 方法 public void setUserName(Strin…

如何设置计算机硬盘密码,计算机设置硬盘加密方法以启动密码

计算机加密通常包括软件加密,系统加密等,但是我们还设置了硬盘加密。这是最先进的加密系统。以下是将硬盘设置为为您启动密码加密的计算机的汇编。方法,希望对您有帮助! 如何在计算机上设置硬盘加密 我的计算机来自联想。引导时按…

云安全与云渗透

一、引言 随着技术的进步,云计算已成为信息技术领域的主流趋势。企业和个人都在利用云服务实现数据存储和处理的便利,但同时也带来了一系列的安全问题。对于这些问题,我们需要深入理解云安全和云渗透的重要性。本文将详细探讨这两个主题。 …

密码及加密技术

密码及加密技术 密码技术概述 现在的学术界一般认为,密码学研究的目的乃是要保证数据的保密性、完整性和认证性。 数据的保密性是指未经授权的用户不可获得原始数据的内容。 数据的完整性是验证数据在传输中未经篡改。 数据的认证(审查)性是指验证当前数据发送方的真…

安全防御——密码学

安全防御——密码学 基本知识1.术语2.保密原则3.安全性4.安全目标5.加密原则6.数学基础7.数学难题 密码类型1.古典密码(1)置换密码(2)代换密码 2.现代密码(1)对称密码(单钥密码)(2)分组密码(块密码)(3)非对称密码(双钥密码)非对称加密的产生过程及原理 (4)单向函数 数字签名与密…