【Vue】axios入门(二)

视频

中文文档

ajax封装

在这里插入图片描述
axios视频

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <style>
      .show{
        padding-left: 30px;
        padding-top: 30px;
        position: absolute;
        top:40px;
        left:50px;
        width: 500px;
        height: 300px;
        background: #f1f1f1;
      }

    </style>
</head>
<body>
  <div class="show">
    <button onclick="testGet()">GET请求</button>
    <button onclick="testPost()">POST请求</button>
    <button onclick="testPut()">PUT请求</button>
    <button onclick="testDelete()">DELETE请求</button>
  </div>

</body>
<script>
  axios({
    url:'http://localhost:8082/depart/findAll'
  }).then(res=>{
    console.log(res);
  })
</script>
</html>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
使用axios.get发送请求
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <style>
      .show{
        padding-left: 30px;
        padding-top: 30px;
        position: absolute;
        top:40px;
        left:50px;
        width: 500px;
        height: 300px;
        background: #f1f1f1;
      }

    </style>
</head>
<body>
  <div class="show">
    <button onclick="testGet()">GET请求</button>
    <button onclick="testPost()">POST请求</button>
    <button onclick="testPut()">PUT请求</button>
    <button onclick="testDelete()">DELETE请求</button>
  </div>

</body>
<script>
  axios.get('http://localhost:8082/depart/findByDname',
    {params:{Dname:'学生部'}})
  .then(res=>{
    console.log(res);
  }).catch(err=>{
    console.log(err)
  })
  
</script>
</html>

axios全局配置

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <style>
      .show{
        padding-left: 30px;
        padding-top: 30px;
        position: absolute;
        top:40px;
        left:50px;
        width: 500px;
        height: 300px;
        background: #f1f1f1;
      }

    </style>
</head>
<body>
  <div class="show">
    <button onclick="testGet()">GET请求</button>
    <button onclick="testPost()">POST请求</button>
    <button onclick="testPut()">PUT请求</button>
    <button onclick="testDelete()">DELETE请求</button>
  </div>

</body>
<script>
  axios.defaults.baseURL = 'http://localhost:8082/depart';
  axios.defaults.timeout=5000;
  axios.get('/findByDname',
    {params:{Dname:"学生部"}})
  .then(res=>{
    console.log(res);
  }).catch(err=>{
    console.log(err)
  })

</script>
</html>

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <style>
      .show{
        padding-left: 30px;
        padding-top: 30px;
        position: absolute;
        top:40px;
        left:50px;
        width: 500px;
        height: 300px;
        background: #f1f1f1;
      }

    </style>
</head>
<body>
  <div class="show">
    <button onclick="testGet()">GET请求</button>
    <button onclick="testPost()">POST请求</button>
    <button onclick="testPut()">PUT请求</button>
    <button onclick="testDelete()">DELETE请求</button>
  </div>

</body>
<script>
  //创建实例
  let newVar = axios.create({
    baseURL:'http://localhost:8082/depart',
    timeout:5000
  })
  newVar({
    url:'/findAll'
  }).then(res=>{
    console.log(res)
  })

</script>
</html>

在vue模块的封装

在这里插入图片描述

import axios from "axios";
export function request(config) {
  axios({
    url:config
  }).then(res=>{
    console.log(res)
  })
}

在这里插入图片描述

import {request} from "./network/request/requset";
request('http://localhost:8082/depart/findAll')

axios常见5种请求方法

SpringMVC请求参数接收总结

post

常用的编码类型有这几种:1.application/x-www-form-urlencoded(常用于原生form表单提交);2.multipart/form-data(常用于文件传输);3.application/json(常用JSON格式编码)

    注意:不同的Content-type需要对应不同的参数格式

          1.application/x-www-form-urlencoded。参数格式:需要将正常的对象转化

          例如:var   data={

                                    name:'小明',

                                    age:'18'

                             }

                   data=qs.stringify(data)

        注意: qs.stringify将对象序列化。使用qs.stringify函数时需要引入qs(通过npm安装)

        2.multipart/form-data。参数格式:form-data

        例如:var    data=new   FormData();

                   data.append("name","小明");

                   data.append("age","18");

        3.application/json。参数格式:JSON对象

        例如:var    data={

                                    "name","小明",

                                    "age","18"

                            }

谈谈axios配置请求头content-type

res.data表示的是后端向前端传输的对象obj
res.data.data表示的是后端向前端传输的对象obj属性名称为data对应的属性值

1、Content-Type: application/json

    test () {
      let temp = {
        'userName': '楠晗',
        'pwd': '123456'
      }
      // temp对应的是请求体的内容,必须是对象,不能是变量
      this.$axios.post('http://localhost:8888/book/query', temp, {
        headers: {
          'Content-Type': 'application/json; charset=utf-8'
        }
      })
        .then(res => {
          console.log(res.data.data)
        }).catch(err => {
          console.log(err)
        })
    }
  }

后端:

    @PostMapping("/query")
    public RestResponse queryTest(@RequestBody User user){
        System.out.println(user);
        return RestResponse.ok("已收到");
    }

2、Content-Type: multipart/form-data

test () {
      let temp = new FormData()
      temp.append('userName', 'hangover')
      temp.append('pwd', '123456')
      this.$axios.post('http://localhost:8888/book/query', temp)
        .then(res => {
          console.log(res.data.data)
        }).catch(err => {
          console.log(err)
        })
    }

在这里插入图片描述

    @PostMapping("/query")
    public RestResponse queryTest(User user){
        System.out.println(user);
        return RestResponse.ok("已收到");
    }

3、Content-Type: application/x-www-form-urlencoded

在这里插入图片描述

npm install qs
<script>
import qs from 'qs'

    test () {
      let temp = {
        'userName': '楠晗',
        'pwd': '123456'
      }
      this.$axios.post('http://localhost:8888/book/query', qs.stringify(temp))
        .then(res => {
          console.log(res.data.data)
        }).catch(err => {
          console.log(err)
        })
    }

</script>

后端:

    @PostMapping("/query")
    public RestResponse queryTest(User user){
        System.out.println(user);
        return RestResponse.ok("已收到");
    }
data(){
        return{
          ruleForm: {
            dname: '',
            index: '',
            manger: '',
            tel:'',
            email:'',
            cadate:'',
            status:''
          }

        }
      }

add(){
 let formdata = new FormData();
          for(let key in this.ruleForm){
            formdata.append(key,this.ruleForm[key]);
          }
}
this.$axios.post('http://localhost:8082/depart/add',formdata)


delete

deleteRow(param){
            this.$axios.delete('http://localhost:8082/user/delete',{
              params:{id:param}
            }).then(successRespose =>{
              this.init();
            }).catch(err=>{
              console.log(param)
            })
        },

get

init(){

            this.$axios.get('http://localhost:8082/user/select')
            .then(res=>{
              this.userData = res.data;
            }).catch(err=>{
              console.log(res.data)
            })
          },

带参数的请求:

findByType () {
      this.$axios.get('http://localhost:8888/book/findByType', {
        params: {
          type: this.bookType // type对应的是后端所需要的参数名称
        }
      })
        .then(res => {
          console.log(res)
          this.tableData = res.data.data
        }).catch(err => {
          console.log(err)
        })
    }

后端:

    @GetMapping("/findByType")
    public RestResponse findByType(@RequestParam("type") String type){
        return RestResponse.ok().put("data",bookService.findBookByType(type));
    }

put

editForm:{
              id:'',
              userName:'',
              passWord:'',
              realName:''
            }
let formData = new FormData();
          for(let key in this.editForm){
            formData.append(key,this.editForm[key])
          }
            this.$axios.put('http://localhost:8082/user/update',formData)
            .then(res=>{
              //this.init();
            })
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值