当前位置: 首页 > news >正文

前后端交互:axios 和 json;springboot 和 vue

vue

准备的

<template><div><button @click="sendData">发送数据</button><button @click="getData">接收</button><button @click="refresh">刷新</button><br><ul v-if="questions"><li v-for="(question ,index) in questions" :key="index"><p>{{ question.problem }}</p><p>{{ question.answer }}</p></li></ul></div></template><script setup>import { ref } from 'vue';import axios from 'axios';const myObject = ref({name: 'John',age: 30,city: 'New York'});const questions =ref()const refresh =()=>{questions.value=null;}const sendData = async () => {try {const jsonString = JSON.stringify(myObject.value);console.log(jsonString);const response = await axios.post('http://localhost:8081/test/login', jsonString, {headers: {'Content-Type': 'application/json'}});console.log('响应:', response.data);} catch (error) {console.error('发送数据时出错:', error);}};const getData = async () => {try {const response = await axios.get('http://localhost:8081/test/reviewmore');questions.value =response.data;console.log('响应:', response.data);} catch (error) {console.error('发送数据时出错:', error);}};</script>
<template><div><button @click="sendMsg">发送msg</button><button @click="getUser">获取一个User</button><ul v-if="users"><li v-for="(user ,index) in users" :key="index"><p>{{ user.name }}</p><p>{{ user.age }}</p><p>{{ user.city }}</p></li></ul></div>
</template><script setup>import axios from 'axios';import {ref} from 'vue';const msg='hello 后端';const sendMsg = async () => {try {const jsonString = JSON.stringify(msg);console.log(jsonString);const response = await axios.post('http://localhost:8081/teach/sendmsg', jsonString, {headers: {'Content-Type': 'application/json'}});console.log('响应:', response.data);} catch (error) {console.error('发送数据时出错:', error);}};const users =ref();const getUser = async () => {try {const response = await axios.get('http://localhost:8081/teach/getusers');console.log('响应:', response.data);users.value=response.data;} catch (error) {console.error('发送数据时出错:', error);}};
</script>

springboot

准备的

package com.example.demo.controller;import com.example.demo.pojo.Question;
import com.example.demo.pojo.User;
import org.springframework.web.bind.annotation.*;import java.util.ArrayList;
import java.util.List;@CrossOrigin
@RequestMapping("/test") // 将公共路径添加到类上
@RestController
public class DemoController {@PostMapping("/login")public String handleLoginRequest(@RequestBody User user) {// 这里可以对接收到的User对象进行处理System.out.println("接收到的用户信息:" + user.getName() + ", " + user.getAge() + ", " + user.getCity());// 返回一个简单的响应return "成功接收到用户信息";}@GetMapping("/get/{id}") // 在路径中定义了一个名为"id"的路径参数public String handleGetRequest(@PathVariable("id") String id) {// 根据接收到的路径参数进行条件查询逻辑// 假设这里根据id查询某个结果,这里只是简单示例if ("1".equals(id)) {System.out.println("查询到ID为1的结果");return "查询到ID为1的结果";} else {System.out.println("未查询到符合条件的结果");return "未查询到符合条件的结果";}}@GetMapping("/review")public Question handleReviewRequest(){return new Question("这是一个问题","这是一个答案");}@GetMapping("/reviewmore")public List<Question> handleReviewMoreRequest() {List<Question> questions = new ArrayList<>();questions.add(new Question("问题1", "答案1"));questions.add(new Question("问题2", "答案2"));questions.add(new Question("问题3", "答案3"));// 添加更多问题return questions;}
}

上课敲的

package com.example.demo.controller;import com.example.demo.pojo.User;
import org.springframework.web.bind.annotation.*;import java.util.ArrayList;
import java.util.List;@CrossOrigin
@RequestMapping("/teach") // 将公共路径添加到类上
@RestController
public class TeachController {@PostMapping("/sendmsg")public String handleLoginRequest(@RequestBody String msg) {System.out.println(msg);return "hello 前端";}@GetMapping("/getusers")public List<User> handleReviewRequest() {List<User> users = new ArrayList<>();users.add(new User("张三", 25,"北京市"));users.add(new User("李四", 30,"唐山"));users.add(new User("王五", 35,"天际"));return users;}}
http://www.lryc.cn/news/359401.html

相关文章:

  • 前端技术专家岗(虚拟岗)
  • redis windows环境下的部署安装
  • 大字体学生出勤记录系统网页HTML源码
  • 筛斗数据提取技术在企业成本预测中的应用
  • enum编程入门:探索枚举类型的奥秘
  • 刷机 iPhone 进入恢复模式
  • 计算属性和侦听器:为什么在某些情况下使用计算属性比使用methods更好,如何使用侦听器来监听数据的变化。
  • 一文带你搞懂大事务的前因后果
  • 关系数据库:关系运算
  • 微信公众号开发(三):自动回复“你好”
  • docker基本操作命令(3)
  • 003 MySQL
  • 数据分析------统计学知识点(一)
  • Apache Doris 基础 -- 数据表设计(分区分桶)
  • 题目:求0—7所能组成的奇数个数。
  • 网络协议学习笔记
  • C语言文件操作:打开关闭,读写
  • 启智CV机器人,ROS,ubuntu 20.04 【最后一步有问题】
  • React-生成随机数和日期格式化
  • 11Linux学习笔记
  • 004 仿muduo实现高性能服务器组件_Buffer模块与Socket模块的实现
  • 研发效能DevOps: Ubuntu 部署 JFrog 制品库
  • hadoop学习笔记
  • 使用dockerfile快速构建一个带ssh的docker镜像
  • linux部署运维1——centos7.9离线安装部署涛思taos2.6时序数据库TDengine
  • Linux shell编程学习笔记51: cat /proc/cpuinfo:查看CPU详细信息
  • Ps:调整画笔工具
  • 香橙派 AIpro上手体验并验证车道线识别算法
  • 为啥装了erlang,还报错erl: command not found?
  • 容器技术基础理论与常用命令:必知必会,效率翻倍!