2017-04-11 2 views
-1

임에서 휴식 API에 대한 HTTP 요청을 가져올 수 없습니다 HTTP GET 젠토에서 만든 나머지 API를 사용하여 각 2 프론트 엔드의 요청 2.은려고 각 2

VAR \ \ www가 \ HTML \ Angular2 샘플 NG \ 같이 나의 시도는 JSON 응답을 얻을 임 수, JSON 파일 (http://10.20.1.2:3000/data.json)을받을 경우 -http 마스터 \ 웹은 \ 응용 프로그램 \

///<reference path="../node_modules/angular2/typings/browser.d.ts" /> 
import "rxjs/add/operator/map"; 
import {Component, Input} from 'angular2/core'; 
import {Http} from 'angular2/http'; 

interface Person { 
    //name: string; 
    //age: number; 
} 

@Component({ 
    selector: 'my-app', 
    template: `<h1>My First Angular 2 App</h1> 
    <div>{{people | json}}</div> 
    ` 
}) 
export class AppComponent { 
    @Input() private people: Person[]; 

    constructor(private http: Http) { 
    } 

public ngOnInit() { 
    this.http.get('http://10.20.1.2:3000/data.json') 
     .map(response => { 
      console.log(response.json); 
      console.log(response.json()); 
      return response.json() 
     }) 
     .subscribe((items: Person[]) => { 
      console.log('items: ' + items); 
      this.people = items; 
     }, error => console.log(error)) 
} 
} 

여기 app.component.ts. 내가이 필요한 것은

내가이 같은 젠토 2의 API 엔드 포인트를 전달해야 내가 이죠 클라이언트에서 호출하는 경우

http://10.20.1.2/Magento2/index.php/rest/V1/customers/1

위의 나머지 엔드 포인트가 완벽하게 작동합니다. 내가 app.component.ts 파일에 동일한 URL (http://10.20.1.2/Magento2/index.php/rest/V1/customers/1)를 사용하는 경우

내가 얻을 :

enter image description here

내가 :(http://10.20.1.2:3000/Magento2/index.php/rest/V1/customers/1으로 포트 3000 URL을 사용하는 경우가) 내가 얻을 :

응답 {_body "/Mage_ang2/index.php/rest/V1/customers/1↵를 얻을 수 없다", 상태 : 404,하는 statusText : "확인"을, 헤더 : 헤더를 입력 : 2 ...

어느 부분이 누락 되었습니까?

+1

당신이 헤더 액세스 제어 - 허용 - 원산지를 설정 했는가를? – giaco

답변

1

당신은 헤더를 설정해야합니다

액세스 제어 - - 원산지 허용 : 'your_angular_server_address'

액세스 제어 - 허용 - 헤더 : '액세스를 - 제어 - 원점 허용 '

이것은입니다.문제입니다.

+0

TL; DR Magento 서버는 다른 서버에서 전화를 걸 수 없습니다. 이 문제를 해결하려면 Access-Control-Allow-Headers를 구성해야합니다. – Stefan

+0

헤더를 어디에 설정해야하는지 알려주십시오. 내 나머지 API URL에 있습니까? – Sach

+0

REST API 응답에 헤더를 설정해야합니다. 여기에는 magento의 예제가 나와 있습니다. http://magento.stackexchange.com/questions/92105/magento-2-sending-a-custom-header-response-from-a-controller 다음 : https://www.kathirvel.com/magento-returning-json-for-ajax-and-api-calls-response-from-controller-action/ – giaco

0
당신은 헤더를 추가 할 필요가

:

/** 
    * Interceptor set the headers for the request. 
    * Params: 
    * - objectToSetHeadersTo: Request or Arguments that will contain the headers. 
    **/ 
    private setHeaders(objectToSetHeadersTo: Request | RequestOptionsArgs) { 
    const headers = objectToSetHeadersTo.headers; 
    headers.set('Content-Type', 'application/json'); 
    if (this.token) { 
     headers.set('Authorization', 'Token ' + this.token); 
    } 
    }