가져 오기 호출에서 맞춤 헤더를 보내려고하지만 어떤 이유로 헤더가 전송되지 않은 것으로 보입니다. 'cors'모드를 가져 오는 옵션으로 설정해야한다는 질문이 몇 가지 있었지만 시도해 보았지만 차이는 없었습니다. 콘솔에서가져 오기에서 맞춤 헤더를 보내지 않습니다.
내가이 오류 받고 있어요 : 내 인출 요청에서 X-API 키 헤더를 제거 할 경우
Fetch API cannot load http://localhost:8000/GroupRoutePermission. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8082' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
을, 나는 어떤 CORS 콘솔 오류와 JSON하지 않습니다 응답 잘 - 내 JSON api 키를 (예상대로) 설정되어 있지 않습니다 오류가 있습니다.
나는 또한 x-api-key가 설정된 우편 배달부로 내 끝점에 도달했습니다. 정상적으로 작동합니다. 이상하게도 나는 이전의 프로젝트에서 아래의 코드를 찢어 버렸고, 그 프로젝트에서 커스텀 헤더는 잘 보냈다. (심지어 cors 모드가 아님), 그 밖의 무엇을 시도해야 할지를 놓치고있다.
let apiKey = ""
if (typeof localStorage.apiKey != 'undefined')
apiKey = localStorage.apiKey
else
window.location = "/login"
console.log(apiKey)
fetch(url,{
credentials: 'include',
mode: 'cors',
headers: new Headers({
'Content-Type': 'text/plain',
'x-api-key': localStorage.apiKey
})
})
크롬 네트워크 탭 요청 헤더 : 전송 X-API-키와
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8,fr-CA;q=0.6,fr;q=0.4,en-CA;q=0.2
Access-Control-Request-Headers:x-api-key
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8000
Origin:http://localhost:8082
Referer:http://localhost:8082/lists/ResearchTrial
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.90 Safari/537.36
응답 헤더 :
HTTP/1.1 200 OK
Host: localhost:8000
Connection: close
X-Powered-By: PHP/5.5.38-4+deb.sury.org~xenial+1
Allow: GET,HEAD
Cache-Control: no-cache
Content-Type: text/html; charset=UTF-8
Date: Tue, 12 Sep 2017 19:30:58 GMT
응답 헤더 내가 요청 X-API-키를 제거하는 경우 :
HTTP/1.1 200 OK
Host: localhost:8000
Connection: close
X-Powered-By: PHP/5.5.38-4+deb.sury.org~xenial+1
Access-Control-Allow-Origin: http://localhost:8082
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Content-Length, Accept- Encoding, X-Api-Key
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Cache-Control: no-cache
Content-Type: application/json
Date: Tue, 12 Sep 2017 19:28:29 GMT
도와주세요!
실제로 복제! 그 질문/답변에 링크 해 주셔서 감사합니다! – user3246127