后端发送请求

一、Vue整合Axios

1.下载Axios:

npm install --save axios vue-axios

2.axios提供了设置根域名的方法。

在main.js全局入口文件中,设置:

import axios from “axios”;
axios.defaults.baseURL = ‘http://localhost:8080’

3.响应拦截

// 添加Axios响应拦截器
axios.interceptors.response.use(function (response) {
//console.log(‘响应拦截’,response)
return response.data;
}, function (error) {
console.log(error)
});

4.在需要使用的页面中引入

import axios from ‘axios’

二、1.当后端发送get请求,此时前端可以使用params传递参数

axios.get(“http://localhost:8080/后端请求路径”,{

params:{

属性:值,

属性:值

}

}).then(res=>{

if (res.code == 20000){  //此处的res就相当于R(因为在main.js中已经设置拦截)
          this.$message({
            message:"登录成功",
            type:"success",
            duration:1000
          })
        }else {
          this.$message({
            message:"登录失败",
            type:"error",
            duration:1000
          })
        }

})

在这里插入图片描述
在这里插入图片描述

前端要将json变成对象,加注解@RequestBody

2.当后端发送post请求,此时后台需要使用@RequestBody

axios.post(‘http://localhost:8080/后端请求路径’,{
username:‘张三’,
password:‘987654’
}).then(function(ret){
console.log(ret.data);
})

axios.put(‘http://localhost:8080/后端请求路径’,{
username:‘张三丰’,
password:‘11111’
}).then(function(ret){
console.log(ret.data);
}).catch(function (err){
console.log(err)
})

方式1:

在这里插入图片描述
在这里插入图片描述

方式2:

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Su sir~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值