2016-10-18 6 views
0

원시 파일에서 코드를 가져 오는 응용 프로그램이 있습니다.개인 은닉 링크에서 텍스트 가져 오기

public getRawFile(rawLink: string) { 
    return this.http.get(rawLink).map((res: Response) => res.text()); 
    } 

을하지만 지금은 숨겨 놓은 원시 파일의 코드를 좀하고 싶습니다하지만 개인 저장소 : 공공의 저장소에서 폐기하는 것은 간단하다, 그것은 다음과 같습니다. 사용자가 원시 파일의 소스 코드보다 액세스 (숨김 (stash)에 로그인되어있는 경우)가로드됩니다.

XMLHttpRequest cannot load 'private_stash_file_link'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. 
    EXCEPTION: Response with status: 0 for URL: null 
    Uncaught Response with status: 0 for URL: null 

어떻게 GET 요청이, 쿠키, 특정 옵션을 처리 할 수있는, 그것도 가능하다 :

은 내가 respone를 받고, 같은 방법을 시도하는 경우?

편집 1.

시도 :

public getRawFile(link: string) { 
    let headers = new Headers(); 
    headers.append('Access-Control-Allow-Headers', 'Content-Type'); 
    headers.append('Access-Control-Allow-Methods', 'GET, OPTIONS'); 
    headers.append('Access-Control-Allow-Origin', '*'); 
    let options = new RequestOptions({headers: headers, withCredentials: true}); 
    return this.http.get(link, options).map((res: Response) => res.text()); 
    } 

하지만 개인 저장소에 대한 동일한 결과 ..

plunker

답변

0

당신이 요청을하는 서버에있다합니다 CORS를 구현하여 웹 사이트 액세스에서 JavaScript를 부여하십시오 (Cross Origin Resource Sharing (CORS)). 의 도메인 이름으로 "* .example.com을"을

Access-Control-Allow-Headers: Content-Type 
Access-Control-Allow-Methods: GET, POST, OPTIONS 
Access-Control-Allow-Origin: *.example.com 

것은 교체해야합니다 : 당신은 당신이된다고하는 장소에 액세스 할 수있는 경우 그래서, 다음 수신 측의 응답에 다음 HTTP 헤더를 추가 데이터를 보내는 호스트/데이터를받는 호스트. 이렇게하면 문제가 해결됩니다.

+0

편집 1에서와 같이 시도했지만 작동하지 않습니다. 이제는 공개 상태의 http 상태 400이 표시됩니다. – ulou

+0

요청을받는 서버에 CORS 설정이 있습니까? – pretzo246

+0

https://plnkr.co/edit/ogDayyIOEbOLkI8XBzOX?p=preview – ulou