AJAX(三)——用axios写GET , POST , PUT , DELETE方法

Axios浅了解

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

  • 具备以下特点:

  1. 在浏览器中创建XMLHttpRequest请求
  2. 在node.js中发送http请求
  3. 支持Promise API
  4. 拦截请求和响应
  5. 转换请求和响应数据
  6. 取消要求
  7. 自动转换JSON数据
  8. 客户端支持防止CSRF/XSRF(跨域请求伪造)

用Axios写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');
	})
}

用Axios写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');
	})
}

用Axios写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');
	})
}

用Axios写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');
	})
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值