axios简单练习

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

今天做一个post请求前后端代码练习。实现思路:前端使用axios发送post请求,后端使用过springboot接受请求。

一、get请求

前端代码(vue+axios)

<template>
  <div class="test">
      用户名:<input v-model="username" type="text"/>
      密码:<input v-model="password" type="password"/>
      <button v-on:click="getTest">get测试</button>
  </div>
</template>
<script>
export default {
  name: "test",
  data(){
    return {
      username: ' ',
      password: ' '
    }
  },
  methods:{
    getTest(){
        let that = this;
        that.$axios.defaults.baseURL='http://localhost:8081'//设置请求地址
        that.$axios({
        method: 'get',//请求方式
        url: '/test/getTest?username='+that.username+'&password='+that.password,//请求路径和参数
      }).then((result) => {//回调函数
        console.log(result.data);
      });
    }
  }
}
</script>
<style scoped>
</style>

后端代码(springboot)

package com.example.myblog.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value="/getTest",method={RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public String test(String username,String password){
        System.out.println(username);
        System.out.println("------------");
        System.out.println(password);
        return "get请求测试成功";
    }
}

效果

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

二、post请求

普通参数

前端代码(vue+axios)

<template>
  <div class="test">
      用户名:<input v-model="username" type="text"/>
      密码:<input v-model="password" type="password"/>
      <button v-on:click="getTest">post测试</button>
  </div>
</template>
<script>
export default {
  name: "test",
  data(){
    return {
      username: '',
      password: ''
    }
  },
  methods:{
    getTest(){
        let that = this;
        that.$axios.defaults.baseURL='http://localhost:8081'//设置请求地址
        that.$axios({
        method: 'post',//请求方式
        url: '/test/postTest',//请求路径
        data: {
          username: that.username,
          password: that.password
        }
      }).then((result) => {//回调函数
        console.log(result.data);
      });
    }
  }
}
</script>
<style scoped>
</style>

后端代码(springboot)

package com.example.myblog.controller;

import com.example.myblog.vo.TestVo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value="/postTest",method={RequestMethod.POST})
    @ResponseBody
    @CrossOrigin
    public String test(@RequestBody TestVo testVo){
        System.out.println(testVo.toString());
        return "post请求测试成功";
    }
}

效果

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

formdata参数

前端代码(vue+axios)

<template>
  <div class="test">
      用户名:<input v-model="username" type="text"/>
      密码:<input v-model="password" type="password"/>
      <button v-on:click="getTest">post测试</button>
  </div>
</template>
<script>
export default {
  name: "test",
  data(){
    return {
      username: '',
      password: ''
    }
  },
  methods:{
    getTest(){
        let that = this;
        let formData = new FormData();
        formData.append("username",that.username);
      formData.append("password",that.password);
        that.$axios.defaults.baseURL='http://localhost:8081'//设置请求地址
        that.$axios({
        method: 'post',//请求方式
        url: '/test/postTest',//请求路径
        data: formData
      }).then((result) => {//回调函数
        console.log(result.data);
      });
    }
  }
}
</script>
<style scoped>
</style>

后端代码(springboot)

package com.example.myblog.controller;

import com.example.myblog.vo.TestVo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value="/postTest",method={RequestMethod.POST})
    @ResponseBody
    @CrossOrigin
    public String test(TestVo testVo){
        System.out.println(testVo.toString());
        return "post请求测试成功";
    }
}

或者

package com.example.myblog.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value="/postTest",method={RequestMethod.POST})
    @ResponseBody
    @CrossOrigin
    public String test(@RequestParam("username") String username,@RequestParam("password") String password){
        System.out.println(username);
        System.out.println("----------");
        System.out.println(password);
        return "post请求测试成功";
    }
}

效果

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值