vue的axios

axios

官网地址: https://www.npmjs.com/package/axios

axios是什么:

  英文翻译:  Promise based HTTP client for the browser and node.js

  中文翻译:基于Promise的HTTP客户端,用于浏览器和node.js(解决了跨站请求伪造:Cross-site request forgery)

axios的特点:

  1. 从浏览器中创建XMLHttpRequest
  2. 从node.js中创建http请求
  3. 支持Promise API
  4. 拦截请求和响应
  5. 取消请求
  6.  自动转换JSON数据
  7. 客户端支持防止XSRF攻击

下载方式:

  使用 npm:

$ npm install axios

  使用 bower:

$ bower install axios

axios使用方法:

//axios.get()
axios.get('url地址',{
    params:{//入参方式 与动态路由中的params没有关系
    a:1
    }
})
.then((res)=>{
//成功之后的执行逻辑
}
)
.catch(()=>{
    //失败之后执行的逻辑 捕获
})
.finally(()=>{
//不管成功还是失败都会走这个逻辑
})
//axios.post()
axios.post('url地址',{
    //入参
    a:1
})
.then((res)=>{
//成功之后的执行逻辑
}
)
.catch(()=>{
    //失败之后执行的逻辑 捕获
})
.finally(()=>{
//不管成功还是失败都会走这个逻辑
})
//直接调用
axios({
    url:'地址',
    method:'get/post', //get这个传输方式是可以省略的
    params:{//get传递参数的方法

    },
    data:{//post传递参数方式

    }
})
.then(()=>{

})
.catch(()=>{})

jQuery的ajax使用:

  

$.ajax({
    url:'',
    type/method:'get/post',
    data:{},
    success:function(){

    },
    faild:()=>{},
    complate:()=>{
        //不管成功还是失败都走这个逻辑
    }
})
$.ajax.get()
$.ajax.post()

猜你喜欢

转载自www.cnblogs.com/ly1368489670/p/12758505.html