ajax 与 axios区别 axios使用 发起多请求

ajax 与 axios区别
第一种 ajax

var  str='HELLO , 自由网'
	$.ajax({
		url:"test.php",
		data:{str:str},
		type:'post',
		success:function(msg){
			console.log(msg)
		}
	})

Ajax 是一种用于创建快速动态网页的技术
是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术
请求 头

Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,und;q=0.7
Connection: keep-alive
Content-Length: 41
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

application/x-www-form-urlencoded; charset=UTF-8
我们可以看到 ajax FormData 类型

axios 类型的
支持浏览器和node.js
支持promise
能拦截请求和响应
能转换请求和响应数据
能取消请求
自动转换JSON数据
浏览器端支持防止CSRF(跨站请求伪造)


     axios.post(
        'test.php',
    ).then((response=>{
    	console.log(response)
    }))

请求头

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,und;q=0.7
Connection: keep-alive
Content-Length: 0

默认axios提交数据类型是Payload,Content-Type:"application/json"
而后台接不到数据, 此时此刻 我们要将 json 格式转换为 str=1这种格式

	 var params = new URLSearchParams();
    params.append('str', '自由网');
    params.append('pass', '123');
    console.log(params)
     axios.post(
        'test.php',
        params
    ).then((response=>{
    	console.log(response)
    }))

发起多个 axios请求

function axios1(){
	axios.get('test.php?type=1')
}
function axios2(){
	axios.get('test.php?type=2')
}
axios.all([axios1(),axios2()]).then( )

自定义配置发送 config

str = '123'
 axios({
 	method:'post',
 	url:"test.php",
 	data:{
 		str:str
 	}
 })
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值