我想针对 HP Alm Rest API 进行授权,我认为这“应该”有效,但事实并非如此:
function performSignIn(){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password));
fetch(sign_in, {mode: 'no-cors', method:'POST', headers: headers})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
我收到一条有点奇怪的错误消息:
401(找不到请求的凭据。)
这是 HP 文档中的一个示例,使用 XMLHttpRequest 完成:http://alm-help.saas.hpe.com/de/12.53/api_refs/REST/webframe.htm#signin-out_example.htm http://alm-help.saas.hpe.com/de/12.53/api_refs/REST/webframe.htm#signin-out_example.htm
任何想法?我不明白有什么问题。
多谢!
此致,
丹尼尔
fetch(…)
默认情况下不发送凭据。你需要明确指定应发送它们 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Sending_a_request_with_credentials_included:
fetch(sign_in, {credentials: 'include' , method:'POST', headers: headers})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
另外,顺便说一句,你绝对不想指定mode: 'no-cors'
。其作用是告诉浏览器,阻止我的前端 JavaScript 访问此请求的响应。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)