栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

axios简单练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

axios简单练习

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

文章目录
  • 前言
  • 一、get请求
    • 前端代码(vue+axios)
    • 后端代码(springboot)
    • 效果
  • 二、post请求
    • 普通参数
      • 前端代码(vue+axios)
      • 后端代码(springboot)
      • 效果
    • formdata参数
      • 前端代码(vue+axios)
      • 后端代码(springboot)
      • 效果


前言

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

一、get请求 前端代码(vue+axios)



后端代码(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)



后端代码(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)



后端代码(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请求测试成功";
    }
}
效果


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号