[CORS]fetch error 로컬8080 포트에서 가져오는 문제 #127
Unanswered
Jasmin0010
asked this question in
질문과 답변
Replies: 1 comment
-
혹시 해결하셨나요? 저도 계속 같은 오류가 뜨네요... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
p.213~부터입니다.
APiservice.js
`import { API_BASE_URL } from '../app-config';
const ACCESS_TOKEN = 'ACCESS_TOKEN';
export function call(api, method, request) {
let headers = new Headers({
'Content-Type': 'application/json',
});
// 로컬 스토리지에서 ACCESS TOKEN 가져오기
const accessToken = localStorage.getItem('ACCESS_TOKEN');
if (accessToken && accessToken !== null) {
headers.append('Authorization', 'Bearer ' + accessToken);
}
let options = {
headers: headers,
url: API_BASE_URL + api,
method: method,
};
if (request) {
// GET method
options.body = JSON.stringify(request);
}
return fetch(options.url, options)
.then((response) => {
if (!response.ok) {
return Promise.reject(response);
}
return response;
})
.catch((error) => {
// 추가된 부분
console.log(error.status);
if (error.status === 403) {
window.location.href = '/login'; // redirect
}
return Promise.reject(error);
});
}
export function signin(userDTO) {
return call('/auth/signin', 'POST', userDTO).then((response) => {
if (response.token) {
// 로컬 스토리지에 토큰 저장
localStorage.setItem(ACCESS_TOKEN, response.token);
// token이 존재하는 경우 Todo 화면으로 리디렉트
window.location.href = '/';
}
});
}
export function signout() {
localStorage.setItem(ACCESS_TOKEN, null);
window.location.href = '/login';
}
export function signup(userDTO) {

return call('/auth/signup', 'POST', userDTO);
}
`
코드에서 라우트 코드를 짜고 응답을 받으면 자동으로 로그인 창으로 넘어가야 하는데 로딩중인 화면에서 넘어가질 않습니다.
프론트 코드의 문제가 맞는지 잘 모르겠습니다.
json 대신 response로 받고, spring boot 버전은 2.7.2로 맞춰 적합해 보입니다.
추가로 코드를 수정해주면서 다시 화면이 렌더링되었는데, 라우팅 코드도 switch -> route로 버전에 맞게 바꿔 주는 등의 시도를 했습니다.
현재 403 상태로 백엔드에서 요청 거부한 상태입니다.
백엔드에서 WebMvcConfig 파일에 설정도 완료한 상태입니다.
Beta Was this translation helpful? Give feedback.
All reactions