【问题标题】:Getting http status 401 unauthorized for the web request?为 Web 请求获取 http 状态 401 未经授权?
【发布时间】:2018-02-12 11:35:35
【问题描述】:

我正在使用授权标头调用服务,但我得到了 http 状态代码 401 未经授权的访问,有趣的是,如果我用 chrome 或 postman 尝试这个,我得到的正是结果,这意味着服务器上没有问题侧,那么我在客户端缺少什么,以至于它在选项方法上失败了?

这是我的代码:-

 this.http.get(url,{headers:this.headers}).subscribe(res=>{
    console.log(res);
    });

【问题讨论】:

  • 尝试设置this.http.get(url,{headers:this.headers, withCredentials: true}).subscribe(res=>{ console.log(res); });
  • 我已经尝试过了,但仍然遇到同样的问题。

标签: javascript angular api web-services


【解决方案1】:

对不起,我没有足够的声誉来发表评论,所以我必须回复。

如果您安装 Chors Extension for Chrome 并成功发布,那么问题很可能是因为您试图从本地主机访问您无权访问的服务。

我推荐的解决方案是允许服务器上的所有相关标头,例如(伪):

Access-Control-Allow-Origin: localhost:8888

Access-Control-Allow-Methods: GET, POST, PUT, DELETE

Access-Control-Allow-Headers: Content-Type

如果您可以访问接收服务器,最简单的解决方法是添加

Access-Control-Allow-Origin: *

到您的服务器 (taken from here)。

See Mozilla resources for further reading

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 2013-01-01
    • 1970-01-01
    • 2012-05-03
    • 2020-08-12
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多