首页 > 其他分享 >axios基本用法

axios基本用法

时间:2022-10-21 14:48:49浏览次数:84  
标签:基本 function axios console log res 用法 type

什么是Axios
Axios本质上还是对原生XMLHttpRequest的封装,可用于浏览器和nodejs的HTTP客户端,只不过它是基于Promise的,符合最新的ES规范。

一. axios的三种写法

// 第一种写法
axios.get('/query?name=tom').then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});
// 第二种写法
axios.get('/query', {
    params: {
        name: 'tom'
    }
}).then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});
// 第三种写法
axios({
  method: 'get',
  url: '/query',
  data: {
    name: 'tom',
  }
}).then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});

二. 各种请求的写法

1.get
function axiosGet() {
	axios({
		method: 'get',
		url: 'https://music.benwebsite.cloud/banner',
		headers: {
			'content-type': 'application/json'
		} ,
		params: {
			type: 2 
		}
	}).then(res => {
		console.log('成功');
		console.log(res);
	}).then(err => {
		console.log('err');
	})
}

2.post
function axiosPost() {
	axios({
		method: 'post',
		url: 'https://music.benwebsite.cloud/banner',
		headers: {
			'content-type': 'application/json'
		} ,
		params: {
			type: 2 
		}
	}).then(res => {
		console.log('成功');
		console.log(res);
	}).then(err => {
		console.log('err');
	})
}

3.put
function axiosPut() {
	axios({
		method: 'put',
		url: 'https://music.benwebsite.cloud/banner',
		headers: {
			'content-type': 'application/json'
		} ,
		params: {
			type: 2 
		}
	}).then(res => {
		console.log('成功');
		console.log(res);
	}).then(err => {
		console.log('err');
	})
}

4.delete
function axiosDelete() {
	axios({
		method: 'delete',
		url: 'https://music.benwebsite.cloud/banner',
		headers: {
			'content-type': 'application/json'
		} ,
		params: {
			type: 2 
		}
	}).then(res => {
		console.log('成功');
		console.log(res);
	}).then(err => {
		console.log('err');
	})
}

标签:基本,function,axios,console,log,res,用法,type
From: https://www.cnblogs.com/regit/p/16813397.html

相关文章

  • Java语言深入:深入研究Java equals方法,equals,==,equals用法
    网上关于equals和==的区别的讨论巨多这里先简单分析下他们的区别吧:equals方法(是String类从它的超类Object中继承的)被用来检测两个对象是否相等,即两个对象的内容是否相等......
  • 反编译工具jad简单用法
      反编译工具jad简单用法  以下假设jad.exe在c:/java目录下一、基本用法Usage:   jad[option(s)]<filename(s)>直接输入类文件名,且支持通配符,如下所示。c:/ja......
  • Oracle LISTAGG() 聚合查询用法 GROUP BY 和 PARTITION BY 的使用和比较
    基本语法:LISTAGG(XXX,XXX)WITHINGROUP(ORDERBYXXX)。可以配合GROUPBY进行聚合查询,也可以配合PARTITIONBY进行聚合查询。直接上例子。TEST_USER表记录如下......
  • 关于js中window.location.href,location.href,parent.location.href,top.location.hre
      关于js中"window.location.href"、"location.href"、"parent.location.href"、"top.location.href"的用法   "window.location.href"、"location.href"是本页面跳转......
  • yii2 中 linslin\Curl的基本使用
     yii2中linslin\Curl的基本使用一、get请求:1.1简单get请求uselinslin\yii2\curl;$curl=newcurl\Curl();//gethttp://example.com/get请求改网址$respons......
  • C语言基本结构
    第一个C语言程序#include<stdio.h>main(){printf("Hello,World!\n");}保存文件为hello.c编译cchello.c运行./a.out如下➜clangcodecchello.......
  • expect用法
    expect用法(实例讲解)ly_qiu于2020-04-2522:31:04发布4561收藏12分类专栏:shell版权shell专栏收录该内容16篇文章1订阅订阅专栏expect用法expect 自动应答命令(用......
  • @PostMapping和@GetMapping用法详解
    publicclassApplyObject{privateStringid;privateStringname;}1、使用post方法调用前端传递参数如果是一个object的话,如{id:'1',name:'2222'}后......
  • Linux Crontab命令定时任务基本语法与操作教程
    一、Crontab查看编辑重启  1、查看crontab定时执行任务列表  crontab-l  2、编辑crontab定时执行任务  crontab-e  3、删除crontab......
  • Mybaits基本使用
    Mybaits基本使用整体的项目逻辑第一步:配置mybatis-config.xml核心配置文件,导入项目依赖<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationP......