2017-09-12 7 views
0

가져 오기 호출에서 맞춤 헤더를 보내려고하지만 어떤 이유로 헤더가 전송되지 않은 것으로 보입니다. '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 

도와주세요!

+0

실제로 복제! 그 질문/답변에 링크 해 주셔서 감사합니다! – user3246127

답변

0

I've ripped the below code out of a previous project of mine, and in that project the custom header gets sent just fine (even without cors mode), so I'm at a loss as to what else to try.

해당 프로젝트가 도메인 간 요청을 했습니까?

내 생각에 API는 인증 실패시 API가 cors 헤더를 보내고 인증 성공시 헤더를 보내지 않습니다. 이것은 cors를 걱정할 필요가없는 Postman에 영향을 미치지 않습니다.

인증 된 요청을 보내고 응답 헤더를 확인하여 우체부에서이를 확인할 수 있습니다.

+0

예, 프로젝트가 교차 도메인이기도합니다. > auth가 실패하면 API가 cors 헤더를 보내고 auth가 성공하면 헤더를 보내지 않을 것이라고 추측합니다. 하지만 백엔드에서 Laravel을 사용하고 있으며 Cors와 Api key auth에 대해 별도의 미들웨어가 있습니다. Cors는 요청을 받으면 실행하는 첫 번째 프로그램입니다. – user3246127

+0

나는 Postman을 다시 시험해 보았는데 api 키가 포함되어 있고 포함되지 않은 상태로 요청을 실행했다. – user3246127

+0

응답의 헤더는 인증에 관계없이 동일합니까? Chrome (또는 다른 브라우저)에서도 동일합니까? – Sidney