【发布时间】:2016-10-02 15:37:28
【问题描述】:
我有一个休息网络服务,现在我想从 ionic 2 前端应用程序向身份验证休息方法发出一个发布请求。
在我的登录组件上我有:
...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);
}
我还有一个私有方法来设置标题:
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
我做错了什么?
【问题讨论】:
-
看到这个伟大的蒂埃里的回答stackoverflow.com/questions/36768418/…
标签: http angular xmlhttprequest ionic2