axios案例

 

 使用说明:参考 https://www.kancloud.cn/yunye/axios/234845

 跨域解决:参考 https://blog.csdn.net/u012369271/article/details/72848102

 使用心得:参考 https://mp.weixin.qq.com/s/Gnv2LVY8AHIBT7Z5Iq5RXg

  以下是简单demo截图:

    

 

     

 

     

 

     

 

     

 

   以下是:案例源码

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>axiosdemo</title>
</head>

<body>
<button οnclick="getaxios()">getaxios</button>
<button οnclick="postaxios()">postaxios</button>
<button οnclick="comaxios()">comaxios</button>
<button οnclick="allaxios()">allaxios</button>
<button οnclick="cusAxios()">cusAxios</button>
<button οnclick="interceptor()">interceptor</button>

</body>

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

<script>
function interceptor() {
var instance = axios.create({
url: './axios.php',
timeout: 3000,
method: 'post',
params: {
type: 'top'
}
});
instance.interceptors.request.use(config => {
console.log(111);
// 设置一些加载动画等操作
return config;
});
// .get .post
instance.request().then(function(data) {
console.log(data);
}).catch();
}

function cusAxios() {
var instance = axios.create({
url: './axios.php',
timeout: 3000,
method: 'post',
params: {
type: 'top'
}
});
instance.get();
instance.request();
}

// 多个并发请求
function allaxios() {
function getAxios1() {
return axios.get('./axios.php?type=top');
}

function getAxios2() {
return axios.get('./axios.php?type=yule');
}

axios.all([getAxios1(), getAxios2()]).then(axios.spread(function(data1, data2) {
console.log(data1);
console.log(data2);
}))
}

function comaxios() {
// 通过config对象来配置axios请求
axios({
method: 'post',
url: './axios.php',
data: {
type: 'top'
}
})
.then(function(response) {
console.log(response);
})
.catch(function() {

})
}

function postaxios() {
axios.post('./axios.php', {
type: 'top'
})
.then(function(response) {
console.log(response);
})
.catch(function() {})
}

function getaxios() {
axios.get('./axios.php', {
params: {
type: 'top'
}
})
.then(function(response) {
console.log(response);
})
.catch(function(err) {

});
}
</script>

</html>

  

 

转载于:https://www.cnblogs.com/TuringShine/p/11601842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue2中,可以利用axios进行网络请求的操作。下面是一个使用axios案例: ```javascript // 引入axios库 import axios from 'axios' // 创建Vue实例 new Vue({ methods: { // 定义一个方法来发送请求 async fetchData() { try { // 使用axios发送GET请求获取数据 const response = await axios.get('/api/data') // 打印返回的数据 console.log(response.data) } catch (error) { // 发生错误时打印错误信息 console.error(error) } } }, // 在Vue的生命周期钩子中调用fetchData方法 created() { this.fetchData() } }) ``` 在这个案例中,我们首先需要引入axios库。接着,在Vue的方法中定义一个async函数fetchData,在这个函数中使用await关键字来发送GET请求获取数据。如果请求成功,我们将打印返回的数据,如果发生错误,则打印错误信息。 请注意,这只是一个简单的案例,实际使用中可能需要配置axios的请求头、超时时间等参数,以及处理其他类型的请求。 需要注意的是,async和await关键字是ES6中的新特性,用于简化异步操作的代码结构。在上述案例中,通过使用async函数和await关键字,可以使代码更加简洁易读。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [VUE-Axios案例](https://blog.csdn.net/weixin_53244569/article/details/119953557)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值