【发布时间】: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