【问题标题】:No 'Access-Control-Allow-Origin' header is present on the requested resource - Ionic 2 [duplicate]请求的资源上不存在“Access-Control-Allow-Origin”标头-Ionic 2 [重复]
【发布时间】: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

我做错了什么?

【问题讨论】:

标签: http angular xmlhttprequest ionic2


【解决方案1】:

事实上,这是服务器端的问题,而不是 Angular2 的问题。预检的 OPTIONS 请求需要在其响应中返回 Access-Control-Allow-Origin 标头。

有关详细信息,请参阅这些文章:

【讨论】:

  • 有什么我可以做的,所以我们可以在客户端绕过它,因为我不拥有服务器。
猜你喜欢
  • 1970-01-01
  • 2017-11-20
  • 2018-03-09
  • 2017-07-21
  • 2020-07-11
  • 1970-01-01
  • 2016-08-17
  • 2014-04-09
相关资源
最近更新 更多