angular 2 XMLHttpRequest无法加载预检的响应具有无效的HTTP状态代码400

时间:2017-04-12 10:57:03

标签: apache angular

已启用CORS以在我的angular2和magento之间启用请求。

我需要更改我的magento的apache服务器设置。

我已添加以下行以在apache的htaccess文件中启用OPTIONS请求。

Header always set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
....

Options +FollowSymLinks
RewriteEngine on
#RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
RewriteRule .* - [L,R=405]

这就是我在角度2中调用我的休息api的方式:

export class ProductService {
public _productUrl = 'http://10..../Mage_ang2/index.php/rest/V1/customers/1';

constructor(private _http: Http) { }

getProducts(): Observable<IProduct[]> {
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
headers.append('Authorization', 'Bearer ntthnrbj1uam2tuv1ekva7n8jh18mcnkby3');
let options = new RequestOptions( {method: RequestMethod.Get, headers: headers });


console.log(headers);
    return this._http.get(this._productUrl,options)
        .map((response: Response) => <IProduct[]> response.json())
        .do(data => console.log('All: ' +  JSON.stringify(data)))
        .catch(this.handleError);
}

如果我运行我的angular2,我得到:

  

XMLHttpRequest无法加载http://10.2..../Magento2/index.php/rest/V1/customers/1。预检的响应具有无效的HTTP状态代码400

从其他域获取响应的正确方法是什么?允许CORS使用上述设置?

0 个答案:

没有答案