axios和AJAX区别 / axios的介绍

axios vs AJAX

  1. Ajax是通过浏览器后台与服务器通信的技术
  2. jQuery中的AJAX只是基于jQuery的实现
  3. 在Vue.js中使用jQuery不能充分利用Vue.js的特性

什么是axios

  • 基于Promise的HTTP库
  • 支持Node.js和浏览器

浏览器:XMLHttpRequest
Node.js:http

关于Promise

  • 一种异步解决的方案
  • Promise一直存在,只是在ES6 时,才被并入了标准库
  • 处理回调
//then 函数的使用
new Promise((resolve,reject) => {
	//业务逻辑
	resolve(1);
{).then(res => {
	//
})then(res => {
	//
})
  • 处理异常
catch函数的使用
getJSON('/post/1.json').then(function(post) {
 return getJSON(post.commentURL);
}).then(function(comments) {
 // some code
}).catch(function(error) {
 // 处理前面三个Promise产生的错误
});

axios的安装

  • 安装到dependencies (生产环境依赖)
    npm install axios --save
    npm install axios -S
  • 安装到devDependencies (开发环境依赖)
    npm install axios --save-dev
    npm install axios -D

如何使用axios

  • axios(config):通用/最本质的发任意类型请求的方式
  • axios(url[, config]):可以只指定 url 发 get 请求
  • axios.request(config):等同于 axios(config)
  • axios.get(url[, config]):发 get 请求
  • axios.delete(url[, config]):发 delete 请求
  • axios.post(url[, data, config]):发 post 请求
  • axios.put(url[, data, config]):发 put 请求
  • axios.defaults.xxx:请求的默认全局配置

你可能感兴趣的:(前端,javascript,前端,css3)