【问题标题】:Get text from private stash link从私人隐藏链接获取文本
【发布时间】:2017-02-27 22:05:09
【问题描述】:

我有从存储原始文件中获取代码的应用程序。从公共存储库中报废很简单,如下所示:

  public getRawFile(rawLink: string) {
    return this.http.get(rawLink).map((res: Response) => res.text());
  }

但现在我想从存储原始文件中获取代码,但来自私有存储库。如果用户有权访问(登录到存储),则加载原始文件中的源代码。

如果我尝试同样的方式,我会得到回应:

    XMLHttpRequest cannot load 'private_stash_file_link'. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
    EXCEPTION: Response with status: 0  for URL: null
    Uncaught Response with status: 0  for URL: null

我该如何处理这个,cookie,获取请求的特定选项,甚至可能吗?

编辑 1.

试过了:

  public getRawFile(link: string) {
    let headers = new Headers();
    headers.append('Access-Control-Allow-Headers', 'Content-Type');
    headers.append('Access-Control-Allow-Methods', 'GET, OPTIONS');
    headers.append('Access-Control-Allow-Origin', '*');
    let options = new RequestOptions({headers: headers, withCredentials: true});
    return this.http.get(link, options).map((res: Response) => res.text());
  }

但私有存储库的结果相同..

plunker

【问题讨论】:

    标签: javascript angular typescript http-headers httpresponse


    【解决方案1】:

    您向其发出请求的服务器必须实施 CORS 以授予 JavaScript 从您的网站访问权限(跨源资源共享 (CORS))。所以如果你可以访问你正在抓取的地方,那么在接收端的响应中添加以下HTTP头:

    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Methods: GET, POST, OPTIONS
    Access-Control-Allow-Origin: *.example.com
    

    确保将“*.example.com”替换为发送数据/获取数据的主机的域名。这样做应该可以解决您的问题。

    【讨论】:

    • 我在 EDIT 1 中尝试过,但它不起作用,还有什么现在我得到了公共 repo 的 http 状态 400,我有什么问题吗?
    • 您确定接收您请求的服务器已设置 CORS 吗?
    猜你喜欢
    • 2018-10-27
    • 2013-04-15
    • 1970-01-01
    • 2013-03-17
    • 2015-07-12
    • 2015-03-31
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多