当前位置:首页 > 前端 > 正文内容

ajax库Axios的使用方法

zhangsir3年前 (2022-10-27)前端377

axios简介

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

axios的基本使用

如何引入axios

可以通过npm安装来进行使用

npm install axio


也可以使用 bower进行安装,然后在页面中进行引入:

bower install axios


还可以使用CDN来进行引入到页面中去

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

如何应用axios

当我们引入了axios之后,接下来我们就要开始正式的使用axios了,对于axios的使方法比较多:

get请求:


// 为给定 ID 的 user 创建请求
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);
  });


post 请求


axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });


zhangsir版权f8防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:http://mianka.xyz/post/51.html

标签: 前端ajax
分享给朋友:

“ajax库Axios的使用方法” 的相关文章

Autojs获取GPS定位信息

记得先把autojs的“定位权限”给开了!!!!console.show(); importClass(android.content.BroadcastReceiver); importClass(android.content.Intent); importClass(android.co...

css如何设置input聚焦时的样式

input的聚焦时的样式怎么设置呢?input:focus-visible {   background-color: red; }这样应该就行了。...

h5 input 设置必填项

html5新增了一个required属性,可以使用这个属性对文本框设置必填项,直接在input文本框上添加required即可 。例如:<input type="text" name="name" placeholder=&...

autojs强制关闭APP详解

// 测试手机为红米note10 pro,autojsPro版本8.8.22-common killApp("微信"); function killApp(appName) {//填写包名或app名称都可以   &...

箭头函数的写法与定义与特点

箭头函数是一种JavaScript函数的语法简写形式。它是ES6(ECMAScript 2015)中的一个新特性,也称为Lambda表达式。箭头函数使用箭头(=>)来定义函数,它的基本语法格式如下:(parameters) => { statements&n...

jquery如何获取最后一个元素

jquery如何获取最后一个元素获取最后一个元素,并将文字设置为红色$(document).ready(function(){     $(p).last().css(color,red); });相关扩展:使用first()方法获取第一个元素#获取第一个...