IT评测·应用市场-qidao123.com

标题: 前后端交互:axios 和 json;springboot 和 vue [打印本页]

作者: 三尺非寒    时间: 2024-6-22 12:56
标题: 前后端交互:axios 和 json;springboot 和 vue
vue

准备的
  1. <template>
  2.     <div>
  3.       <button @click="sendData">发送数据</button>
  4.       <button @click="getData">接收</button>
  5.       <button @click="refresh">刷新</button>
  6.       <br>
  7.       <ul v-if="questions">
  8.         <li v-for="(question ,index) in questions" :key="index">
  9.           <p>{{ question.problem }}</p>
  10.           <p>{{ question.answer }}</p>
  11.         </li>
  12.       </ul>
  13.     </div>
  14.   </template>
  15.   
  16.   <script setup>
  17.   import { ref } from 'vue';
  18.   import axios from 'axios';
  19.   
  20.   const myObject = ref({
  21.     name: 'John',
  22.     age: 30,
  23.     city: 'New York'
  24.   });
  25.   const questions =ref()
  26.   const refresh =()=>{
  27.     questions.value=null;
  28.   }
  29.   const sendData = async () => {
  30.     try {
  31.       const jsonString = JSON.stringify(myObject.value);
  32.       console.log(jsonString);
  33.       const response = await axios.post('http://localhost:8081/test/login', jsonString, {
  34.         headers: {
  35.           'Content-Type': 'application/json'
  36.         }
  37.       });
  38.       console.log('响应:', response.data);
  39.     } catch (error) {
  40.       console.error('发送数据时出错:', error);
  41.     }
  42.   };
  43.   const getData = async () => {
  44.     try {
  45.   
  46.       const response = await axios.get('http://localhost:8081/test/reviewmore');
  47.       questions.value =response.data;
  48.       console.log('响应:', response.data);
  49.   
  50.     } catch (error) {
  51.       console.error('发送数据时出错:', error);
  52.     }
  53.   };
  54.   </script>
  55.   
复制代码
  1. <template>
  2.     <div>
  3.         <button @click="sendMsg">发送msg</button>
  4.         <button @click="getUser">获取一个User</button>
  5.         <ul v-if="users">
  6.             <li v-for="(user ,index) in users" :key="index">
  7.                 <p>{{ user.name }}</p>
  8.                 <p>{{ user.age }}</p>
  9.                 <p>{{ user.city }}</p>
  10.             </li>
  11.         </ul>
  12.     </div>
  13. </template>
  14. <script setup>
  15.     import axios from 'axios';
  16.     import {ref} from 'vue';
  17.     const msg='hello 后端';
  18.     const sendMsg = async () => {
  19.     try {
  20.       const jsonString = JSON.stringify(msg);
  21.       console.log(jsonString);
  22.       const response = await axios.post('http://localhost:8081/teach/sendmsg', jsonString, {
  23.         headers: {
  24.           'Content-Type': 'application/json'
  25.         }
  26.       });
  27.       console.log('响应:', response.data);
  28.     } catch (error) {
  29.       console.error('发送数据时出错:', error);
  30.     }
  31.   };
  32.   const users =ref();
  33.   const getUser = async () => {
  34.     try {
  35.       const response = await axios.get('http://localhost:8081/teach/getusers');
  36.       console.log('响应:', response.data);
  37.         users.value=response.data;
  38.     } catch (error) {
  39.       console.error('发送数据时出错:', error);
  40.     }
  41.   };
  42. </script>
复制代码
springboot

准备的
  1. package com.example.demo.controller;
  2. import com.example.demo.pojo.Question;
  3. import com.example.demo.pojo.User;
  4. import org.springframework.web.bind.annotation.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. @CrossOrigin
  8. @RequestMapping("/test") // 将公共路径添加到类上
  9. @RestController
  10. public class DemoController {
  11.     @PostMapping("/login")
  12.     public String handleLoginRequest(@RequestBody User user) {
  13.         // 这里可以对接收到的User对象进行处理
  14.         System.out.println("接收到的用户信息:" + user.getName() + ", " + user.getAge() + ", " + user.getCity());
  15.         // 返回一个简单的响应
  16.         return "成功接收到用户信息";
  17.     }
  18.     @GetMapping("/get/{id}") // 在路径中定义了一个名为"id"的路径参数
  19.     public String handleGetRequest(@PathVariable("id") String id) {
  20.         // 根据接收到的路径参数进行条件查询逻辑
  21.         // 假设这里根据id查询某个结果,这里只是简单示例
  22.         if ("1".equals(id)) {
  23.             System.out.println("查询到ID为1的结果");
  24.             return "查询到ID为1的结果";
  25.         } else {
  26.             System.out.println("未查询到符合条件的结果");
  27.             return "未查询到符合条件的结果";
  28.         }
  29.     }
  30.     @GetMapping("/review")
  31.     public Question handleReviewRequest(){
  32.         return new Question("这是一个问题","这是一个答案");
  33.     }
  34.     @GetMapping("/reviewmore")
  35.     public List<Question> handleReviewMoreRequest() {
  36.         List<Question> questions = new ArrayList<>();
  37.         questions.add(new Question("问题1", "答案1"));
  38.         questions.add(new Question("问题2", "答案2"));
  39.         questions.add(new Question("问题3", "答案3"));
  40.         // 添加更多问题
  41.         return questions;
  42.     }
  43. }
复制代码
上课敲的
  1. package com.example.demo.controller;
  2. import com.example.demo.pojo.User;
  3. import org.springframework.web.bind.annotation.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. @CrossOrigin
  7. @RequestMapping("/teach") // 将公共路径添加到类上
  8. @RestController
  9. public class TeachController {
  10.     @PostMapping("/sendmsg")
  11.     public String handleLoginRequest(@RequestBody String msg) {
  12.         System.out.println(msg);
  13.         return "hello 前端";
  14.     }
  15.     @GetMapping("/getusers")
  16.     public List<User> handleReviewRequest() {
  17.         List<User> users = new ArrayList<>();
  18.         users.add(new User("张三", 25,"北京市"));
  19.         users.add(new User("李四", 30,"唐山"));
  20.         users.add(new User("王五", 35,"天际"));
  21.         return users;
  22.     }
  23. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4