泸州网站开发,文山网站建设公司,网站底部工信部链接怎么做,北京网站建设 乐云seo1#xff1a;axios作用
axios#xff08;发音#xff1a;艾克c奥斯#xff09;是前端圈最火的#xff0c;专注于数据请求的库。 Axios 是一个基于 promise 的 HTTP 库#xff0c;可以用在浏览器和 node.js 中axios的github:https://github.com/axios/axios
中文官网地址…
1axios作用
axios发音艾克c奥斯是前端圈最火的专注于数据请求的库。 Axios 是一个基于 promise 的 HTTP 库可以用在浏览器和 node.js 中axios的github:https://github.com/axios/axios
中文官网地址http://www.axios-js.com/
英文官网地址https://www.npmjs.com/package/axios
1.1引入axios
首先就是引入axios,如果你使用es6只需要安装axios模块之后 当然也可以用script引入
script srchttps://unpkg.com/axios/dist/axios.min.js/script
2axios的基础语法
2.1发起get请求示例
get请求传参的两种方式
第一种拼接路径上面第二种将参数写在params内
//通过给定的ID来发送请求
axios.get(/user?ID12345).then(function(response){console.log(response);}).catch(function(err){console.log(err);
});
//以上请求也可以通过这种方式来发送
axios.get(/user,{params:{ID:12345}}).then(function(response){console.log(response);}).catch(function(err){console.log(err);
});
2.2发起post请求示例
post请求的示例
axios.post(/user,{firstName:Fred,lastName:Flintstone}).then(function(res){console.log(res);}).catch(function(err){console.log(err);
});
3为什么使用async和await
调用axios方法得到的返回值是Promise对象 注意如果调用某个方法的返回值是Promise实例则前面可以添加await
因为await只能用在被async修饰方法中所有使用await需要带async成对出现
示例 3.1结构响应的属性 通过{对象里面的属性名} 解构出想要的属性
3.2对结构的属性重命名 通过的方式对属性进行重命名
4基于axios.get()和axios.post()发送请求