[JavaScript] CORS 비활성화

조회수 589회

안녕하세요

이번에 하면서 CORS 개념에 대해 알게되었습니다. (로컬에서 index.html 과 app.js를 만들어 localhost:8080에서 실행)

localhost:52652로 요청

이렇게 하면 CORS때문에 안된다는 것을 알았습니다..(원래 cors 때문에 안된다고 하더라구요)

//app.js
fetch('http://localhost:52652/Liveserver/test.json', )
  .then(function(response) {
    console.log(response)
  })
Access to fetch at 'http://localhost:52652/Liveserver/test.json' from origin 'http://127.0.0.1:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

그래서 모드를 no-cors로 변경하면 원하는 데이터가 아닌 opaque가 리턴됩니다..

fetch('http://localhost:52652/Liveserver/test.json', {
    mode: 'no-cors'
})
  .then(function(response) {
    console.log(response)
  })
Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""
__proto__: Response

어떻게하면 CORS를 비활성화 시키면서 fetch를 사용할수 있을까요?

  • 도메인이 달라서 응답을 받지 못하는 상황은 CORS 때문인건 맞는데 정확하게는 CORS [정책에 위배되기] 때문입니다. 그리고 액션을 취해야 할 코드는 클라이언트가 아니라 fetch로 요청할 타겟서버 코드입니다. doodoji 2019.5.25 13:51
  • 모바일이라 자세히 답변드리지 못하는게 아쉽네요 ㅠ doodoji 2019.5.25 13:51

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)