무소의 뿔처럼

서버로 데이터 전송 본문

vue, spring boot/vue

서버로 데이터 전송

값을변경 2025. 8. 13. 16:38

 

  • v-model로 input 값과 Vue 상태를 연결
  • @submit.prevent로 페이지 새로고침 방지
  • axios.post()로 서버로 데이터 전송
import axios from 'axios';
import { ref } from 'vue';


const submitForm = async () => { ... }

<form @submit.prevent="submitForm">

 

@CrossOrigin(origins = "http://localhost:5173")  혹은 config

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://localhost:5173")
                        .allowedMethods("*"); // GET, POST 등 모두 허용
            }
        };
    }
}

 

Spring Boot에서 @RequestBody를 붙여서 JSON으로 받는지 확인하세요:

 

  • 만약 @RequestBody가 없으면 formData를 JSON으로 보낼 때 매핑이 안 됩니다.
  • Content-Type: application/json 으로 전송되므로 @RequestBody 필수입니다.

 

 

'vue, spring boot > vue' 카테고리의 다른 글

File 업로드  (0) 2025.08.13
import { useRoute, useRouter } from 'vue-router'  (1) 2025.08.13
Comments