【问题标题】:How to fix 401 after attempt to override existing POST?尝试覆盖现有 POST 后如何修复 401?
【发布时间】:2020-02-12 19:50:40
【问题描述】:

我在尝试用新数据覆盖现有 POST 时收到 401 错误。它抛出 401。我可以观察到它是 xmlhttprequest。 chrome控制台代码及画面:

const send = function(method,url,data){
    const promise = new Promise(function(resolve,reject){
        const xhr = new XMLHttpRequest();
        xhr.open(method, url)
        xhr.responseType = "json"
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.onload = function(){
            resolve(xhr.response)
        };
        xhr.send(JSON.stringify(data))
    });
    return promise;
};

const final = function(){
    send("POST", "https://www.thecrims.com/api/v1/input",{
        email:"yes",
        password:"no"
    }).then(function(responseData){
        console.log(responseData)
    });
};

【问题讨论】:

  • 错误 401 表示您未获得授权,问题是服务器端的(除非您有凭据并且忘记在请求中包含正确的标头)
  • 我在登录后直接从控制台运行此代码。所以这可能是标题问题?
  • 您应该发送带有所有所需凭据的请求。正确的方法取决于您所针对的 API(询问网站管理员);它可以是请求中发送的身份验证标头或令牌。

标签: javascript ajax xmlhttprequest


【解决方案1】:

您尝试访问的 URL 需要身份验证。您需要提供特定的身份验证才能访问 URL。从服务器端获取并在此处实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-18
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2017-03-26
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多