【问题标题】:401 Unauthorized error for GET request in Ionic 2?Ionic 2 中 GET 请求的 401 未授权错误?
【发布时间】:2016-06-05 02:53:50
【问题描述】:

我有以下获取请求。但它给出了401 Unauthorized 错误。

var headers = new Headers();
        headers.append('Content-Type': 'application/json;charset=UTF-8');

        this.http.get(urgentPostURL + encodeURIComponent(this.urgentpost.comment))
        .map((res:Response) => res.json())
        .subscribe(data => { 

          this.result = data;
          console.log('UrgentPost Result : ' + this.result.Success);

          if (this.result.Success == true) {
              console.log("SUCESSS");
          } else {
              console.log("FAILED");
          }

        },err => console.error(err),() => console.log('done'));

我在这里做错了什么?

已编辑

更新为以下代码后,我仍然收到 401:

var headers = new Headers();
        headers.append('Content-Type': 'application/json;charset=UTF-8');
        headers.append('Authorization', 'Basic ' + btoa("123" + ':' + "123"));
        this.http.get(urgentPostURL + encodeURIComponent(this.urgentpost.comment), { headers: headers })
        .map((res:Response) => res.json())
        .subscribe(data => {

一般:

Request Method:GET
Status Code:401 Unauthorized

响应标头:

Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:0
Date:Tue, 23 Feb 2016 11:26:17 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
Set-Cookie:ARRAffinity=6e6dd0608a0902ef40a800ab07ee37397d7b6cfbd85cf3dea254a7115d365bc1;Path=/;Domain=sd.sddd.net
WWW-Authenticate:Basic
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

请求标头:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Authorization:Basic MDAwMDAwMDoxMjM0NTY3OA==
Connection:keep-alive
Content-Type:application/json;charset=UTF-8
Host:sd.sddd.net
Origin:http://localhost:8100
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36

【问题讨论】:

    标签: web-services http angular ionic2


    【解决方案1】:

    我似乎服务器需要身份验证。您没有为调用定义 Authorization 标头。

    以下是 HTTP 基本身份验证的示例:

    var headers = new Headers();
    headers.append('Content-Type': 'application/json;charset=UTF-8');
    headers.append('Authorizcation', 'Basic '+btoa(username + ':' + password));
    
    this.http.get(urgentPostURL + encodeURIComponent(
              this.urgentpost.comment,
              { headers: headers })
        .map((res:Response) => res.json())
        (...)
    

    不要忘记为您的请求指定标头并导入 Headers 类。

    【讨论】:

    • 谢谢蒂埃里。我有成功的状态(200k),但它抛出了这个错误。 “XMLHttpRequest 无法加载 'my_url'。在预检响应中 Access-Control-Allow-Headers 不允许请求标头字段授权”。这些是我的导入: import 'rxjs/add/operator/map';从 'angular2/http' 导入 { Http, Headers };
    • 太棒了!你在Access-Control-Allow-Headers 里有什么?预检请求(OPTIONS 请求)的响应是否包含Access-Control-Allow-Credentials: true
    • 是的,响应具有 Access-Control-Allow-Credentials: true。
    • 这是我回复的一部分:Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:Authorization, Origin, X-Requested-With, Content-Type, Accept Access- Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Origin:* Content-Length:0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多