请求的资源上不存在 'Access-Control-Allow-Origin' header - Ionic 2
No 'Access-Control-Allow-Origin' header is present on the requested resource - Ionic 2
我有一个休息网络服务,现在我想从 ionic 2 前端应用向身份验证休息方法发出 post 请求。
在我的登录组件上我有:
...this._restClient.post(
'authentication',
body,
(data) => this.handleSuccessAuthenticate(data),
(data) => this.handleErrorAuthenticate(data)
);...
在我的提供商上,我的 _restClient 代码是:
public post(resource: string, data: Object, onSuccess: restClient, onError: callbackRestClient) {
var httpResult: Observable<Response>;
if (data === null) {
httpResult = this._http.post(this.getUrl(resource), '{}', { headers: this.getHeaders() });
} else {
httpResult = this._http.post(this.getUrl(resource), JSON.stringify(data), { headers: this.getHeaders() });
}
this.handleResult(httpResult, onSuccess, onError);
}
我还有一个私有方法可以设置headers:
private getHeaders() {
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
headers.append("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
return headers;
}
我有经典留言:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
我做错了什么?
事实上,这是 server-side 问题而不是 Angular2 问题。预检 OPTIONS 请求需要在其响应中 return Access-Control-Allow-Origin
header。
有关详细信息,请参阅这些文章:
我有一个休息网络服务,现在我想从 ionic 2 前端应用向身份验证休息方法发出 post 请求。
在我的登录组件上我有:
...this._restClient.post(
'authentication',
body,
(data) => this.handleSuccessAuthenticate(data),
(data) => this.handleErrorAuthenticate(data)
);...
在我的提供商上,我的 _restClient 代码是:
public post(resource: string, data: Object, onSuccess: restClient, onError: callbackRestClient) {
var httpResult: Observable<Response>;
if (data === null) {
httpResult = this._http.post(this.getUrl(resource), '{}', { headers: this.getHeaders() });
} else {
httpResult = this._http.post(this.getUrl(resource), JSON.stringify(data), { headers: this.getHeaders() });
}
this.handleResult(httpResult, onSuccess, onError);
}
我还有一个私有方法可以设置headers:
private getHeaders() {
var headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE");
headers.append("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
return headers;
}
我有经典留言:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
我做错了什么?
事实上,这是 server-side 问题而不是 Angular2 问题。预检 OPTIONS 请求需要在其响应中 return Access-Control-Allow-Origin
header。
有关详细信息,请参阅这些文章: