[JavaScript] CORS 비활성화
조회수 591회
안녕하세요
이번에 하면서 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를 사용할수 있을까요?
댓글 입력