vue 3버전 CORS 에러 해결하기

Denia·2023년 12월 14일
0

TroubleShooting

목록 보기
6/24

참고 내용

참고 블로그
Vite 공식 문서

해결 방법

  1. vite.config.js의 파일을 연다
  2. 해당 내용을 추가한다.
// /api로 시작하는 요청에 대해서 proxy를 사용한다.
// origin을 http://localhost:8080 로 변경해서 요청
    server: {
        proxy: {
            '/api': 'http://localhost:8080'
        }
    }
  1. 그럼 다음과 같이 된다.
export default defineConfig({
    plugins: [
        vue(),
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    },
    server: {
        proxy: {
            '/api': 'http://localhost:8080'
        }
    }
})
  1. 서버를 재실행하고 확인해보자.

추가 설명

axios.get("/api/items")
    .then(response => {
      console.log(response.data);
    })
    .catch(error => {
      console.log(error);
    });

다음과 같은 코드가 있고, 설정을 위와 같이 해뒀다면

profile
HW -> FW -> Web

0개의 댓글