-
Client CORS 문제 해결프로그래밍/Javascript 2022. 6. 10. 07:14
에러
Access to XMLHttpRequest at '주소' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
package.json 에 "proxy" 하고 api 요청을 받아오는 서버쪽 호스트 주소를 넣어주면 된다
예를 들어 api 주소가 https://yts.torrentbay.to/api/v2/ 라면
{ /* package.json */ ... "proxy" : "https://yts.torrentbay.to/" }
하고 api 요청하는 쪽에는 /api/v2/ ~~ 하고 남은 url 을 적어주면 된다.
캐시가 남아있어 제대로 동작하지 않을 수 있으므로 node_modules, package-lock.json 지우고 다시 npm run start 하면 제대로 동작할 것이다.
다른 해결방법으로 axios.defaults.withCredentials = true; 가 있었는데 나는 제대로 동작하지 않았다
참조
'프로그래밍 > Javascript' 카테고리의 다른 글
object.keys, values, entries, fromEntries / for...in loop (0) 2021.11.01 webpack (0) 2021.10.25 material-ui 사용기 createTheme, font, style, select (0) 2021.10.15 data 를 typescript enum 으로 매칭하기 (0) 2021.10.14 Array, Object 관련 메소드 (0) 2021.09.07