Im Trying to get HTTP GET request using Rest api of my magento.
Below is the angular2 code to achieve the same!.
public ngOnInit() {
let headers = new Headers({ 'Accept': 'application/json' });
headers.append('Authorization', 'Bearer x9a278mu7xoh4k0jkj08doc5j4b3ac22');
headers.append('Access-Control-Allow-Headers', 'Content-Type');
headers.append('Access-Control-Allow-Methods', 'GET');
headers.append('Access-Control-Allow-Origin', '*');
let options = new RequestOptions({ headers: headers });
return this.http.get('http://10.2.../Mage_ang2/index.php/rest/V1/customers/1',options)
.map(response => console.log(response.json))
.subscribe((items: Person[]) => {
console.log('items: ' + items);
this.people = items;
}, error => console.log(error));
}
I Get (which im assuming) a CORS issue.
OPTIONS http://10.2.../Mage_ang2/index.php/rest/V1/customers/1
XMLHttpRequest cannot load http://10.2.../Mage_ang2/index.php/rest/V1/customers/1. Response for preflight has invalid HTTP status code 500
I have tried the below steps after workaround in google to fix the same, but unsuccessful.
1) In magento .htaccess file, added
Header set Access-Control-Allow-Origin "*"
2) In the above angular 2 code added,
headers.append('Access-Control-Allow-Headers', 'Content-Type'); ...
...
3) Tried changing content-type from application/json in
let headers = new Headers({ 'Accept': 'application/json' });
What am i missing.
Need the proper solution to fix the CORS (from magento2 to angular2) issue.
Suggestions welcome!