IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> axios 使用 (安装方法 get请求 post请求) -> 正文阅读

[JavaScript知识库]axios 使用 (安装方法 get请求 post请求)

目录

1 axios 介绍

2 axios 的使用

2.1 axios 的安装

2.2 axios 的使用方法

2.2.1 axios 的get请求 (基本用法)

2.2.2 axios 的post请求 (基本用法)

2.2.3?axios 的 get 请求 (简写用法)?

2.2.4 axios 的 post 请求 (简写语法)?


?

1 axios 介绍

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。Axios 也是基于 promise 对 Ajax 的封装,axios 主要用于 mvvm 模式,而 Ajax 主要用于 mvc 模式。

2 axios 的使用

2.1 axios 的安装

1 使用 npm :

$ npm install axios

2 使用 bower :

$ bower install axios

3 使用 cdn :

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

2.2 axios 的使用方法

2.2.1 axios 的get请求 (基本用法)

① 使用默认 get 发送无参请求

axios({
    //默认使用 get 方法
      url:'' //这里为所要获取数据的url地址
    }).then(res => {
      console.log(res) //在命令行打印获取数据
    })

② 使用 get 发送无参请求

axios({
      //使用 method 确定使用方法
      method:'get',
      url:''
    }).then(res => {
      console.log(res)
    })

③ 使用默认 get 发送有参请求?

axios({
      // 在 url 地址后面加上 ? ,在 ? 后面加上要传递的参数
      url:'...dong?username=123'
    }).then(res => {
      console.log(res)
    })

?④ 使用 get 发送有参请求

axios({
      // 在 url 地址后面加上 ? ,在 ? 后面加上要传递的参数
      method : 'get',
      url:'...dong?username=123'
    }).then(res => {
      console.log(res)
    })

⑤ 使用 params 发送 get 有参请求

?

axios({
      method : 'get',
      url:'...dong',
      params:{
        // 传递 username 参数 
        username : '123'      
}        
    }).then(res => {
      console.log(res)
    })

2.2.2 axios 的post请求 (基本用法)

① 使用 post?发送无参请求

axios({
      //使用 method 确定使用方法
      method:'post',
      url:''
    }).then(res => {
      console.log(res)
    })

?②?使用 post?发送有参请求(直接使用这种 url 发送的方法不安全)

axios({
      // 在 url 地址后面加上 ? ,在 ? 后面加上要传递的参数
      method : 'post',
      url:'...dong?username=123'
    }).then(res => {
      console.log(res)
    })

③ 使用 params 发送 post?有参请求

axios({
      method : 'post',
      url:'...dong',
      params:{
        // 传递 username 参数 
        username : '123'      
}        
    }).then(res => {
      console.log(res)
    })

?注意:

当不使用 params 而使用 data 传递数据的时候,会发生传递数据为 null 的情况,这是由于 post 请求默认的数据格式是 application/json ,服务器解析不了。

面对这种情况有如下 3 种方法来解决问题:

1.如上所示不使用 data 而使用 params 传递数据,实际上这种方法相当于直接把 params 拼接在 url 后面,也是一种不完全的 post 请求

2.在简写语法中使用 "username = 123"

3.在服务器端进行解决,给接受的参数加上 @requestBody? ? ??

2.2.3?axios 的 get 请求 (简写用法)?

① get 的无参请求

axios.get('...qwe').then(res => {
      console.log(res)
// 使用 catch 处理错误的情况
    }).catch(err => {
      console.log(err)
    })

② get 的有参请求

// 使用 params 来传递参数
axios.get('...qwe',{params:{
      username:1,
      password:2
    }}).then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })

?

2.2.4 axios 的 post 请求 (简写语法)?

① post 的无参请求

axios.post('...qwe').then(res => {
      console.log(res)
// 使用 catch 处理错误的情况
    }).catch(err => {
      console.log(err)
    })

② post 的有参请求

// 直接使用 "username = 123" ,使用 params 会取 null
axios.get('...qwe',"username = 123&age = 10").then(res => {
      console.log(res)
    }).catch(err => {
      console.log(err)
    })

?

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:14:30  更:2021-09-14 13:17:00 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 22:39:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码