Axios简单使用与配置安装-Vue

安装Axios

npm i axios

main.js 导入

javascript 复制代码
import Axios from 'axios'
Vue.prototype.$axios = Axios

简单发送请求

get

javascript 复制代码
getTest() {
        this.$axios({
          method: 'GET',
          url: 'https://apis.jxcxin.cn/api/title?url=https://apis.jxcxin.cn/'
        }).then(res => {
        //请求成功回调
          console.log(res)
        })
      }

post

javascript 复制代码
postTest() {

 //  let data = new URLSearchParams() 表单方式
        this.$axios({
          method: 'POST',
          url: 'https://service3.91suke.com/sk/login',
          //data:data 表单方式
          data:
          {
            id: 120,
            name: '123'
            //post请求参数  这种写法是json格式的
            //		 {id:120,name:'123' }
          }
        }).then(res => {//请求成功回
          console.log(res)
        })
      }

put

javascript 复制代码
 putTest() {
        this.$axios({
          method: 'PUT',
          url: 'www.xxxxx.com/alter',
          data:
          {
            id: 123
          }
        }).then(res => {

          consoloe.log(res)
        })
      }

delete

javascript 复制代码
deleteTest() {
        this.$axios({
          method: 'DELETE',
          url: 'www.xxxxx.com/alter?id=100',
        }).then(res => {
          consoloe.log(res)
        })
      }

方法发送请求(推荐)

get

axios.get(url,params)

params,confing 非必须,参数可以直接在url拼接

javascript 复制代码
        this.$axios.get('https://apis.jxcxin.cn/api/title?url=https://apis.jxcxin.cn/',
          {
            params: { //请求参数对象 xxxxx?id=10
              id: 10
            }
          }).then(res => {
           console.log(res)
          })

post

javascript 复制代码
this.$axios.post('https://service3.91suke.com/sk/login',
          {
            id: 123,
            name: '哈哈哈'

          }).then(res => {
            consoloe.log(res)
          })

其他方法一样

相关推荐
JS菌4 分钟前
Skills 动态加载系统:让 AI Agent 按需获取领域知识
前端·人工智能·后端
weedsfly7 分钟前
Sass 代码复用完全指南:从变量到模块化
前端
张拭心11 分钟前
Android 17 新特性:后台音频交互限制加强
android·前端
张拭心20 分钟前
Android 17 新特性:ProfilingManager 新触发器
android·前端
黄敬峰28 分钟前
从 XMLHttpRequest 到 JSON 模拟:打通前后端通信的任督二脉
javascript
weixin_4713830328 分钟前
Taro-03-页面生命周期
前端·javascript·taro
张拭心32 分钟前
Android 17 新特性:MessageQueue 无锁实现
android·前端
Asize35 分钟前
数组数据结构底层:从灵活到陷阱
前端·javascript·算法
十九画生35 分钟前
Ajax 入门:用 XHR 理解前后端异步请求
前端·javascript·后端