【问题标题】:AJAX 401 (Unauthorized)AJAX 401(未经授权)
【发布时间】:2018-10-08 13:32:01
【问题描述】:

我正在使用 ajax 来使用 Web 服务。

我的第一个请求是获取身份验证令牌的 POST。 (它正在工作。) 但第二个请求显示“401(未授权)”。 这可能是什么问题?

PS。我在 Chrome 上使用 CORS 扩展。如果我禁用它,我会得到“请求的资源上没有'Access-Control-Allow-Origin'标头”

当我在 Postman(禁用 SSL)上使用相同的令牌时,我的请求成功。 同样的令牌也适用于 python "requests.get(urlShow, headers=bpHeader, verify=False)"。

$.ajax({
type: 'POST',
url: 'url',
data: {"authType" : "password", "username" : "<username>", "password" : "<password>", "tenant" : "<tenant>"},
success: function(bpResponse){
    var bpAuth = "Bearer " + bpResponse.token;
    console.log(bpAuth);
    $.ajax({
        type: "GET",
        url: 'url',
        headers: {"Accept": "application/json", "Authorization": bpAuth},
        success: function(bpResources){
            console.log(bpResources);
        }
    });
  }
});

这是我得到的控制台日志:

【问题讨论】:

  • 您的网络服务似乎不接受您提供的令牌。这取决于服务的实现,您需要如何将令牌传递回 Web 服务。您使用的是什么网络服务?定制的?
  • 使用选项headers,而不是header

标签: jquery ajax rest api


【解决方案1】:

只需使用以下命令运行 chrome:

"PATH:\chrome.exe" --user-data-dir --disable-web-security --ignore-certificate-errors --ignore-urlfetcher-cert-requests

【讨论】:

    猜你喜欢
    • 2018-10-23
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 2023-03-23
    • 1970-01-01
    • 2011-03-06
    • 2018-05-19
    相关资源
    最近更新 更多