我想与 gitlab api 连接以获取一些最近的提交。它在 curl 中工作,但 ajax 调用失败。
我从服务器收到状态代码 401:
{"message":"401 Unauthorized"}
标题中的私有令牌似乎是正确的,所以我不知道可能是什么问题。谢谢您的帮助!
:authority: gitlab.example.de (changed to example domain)
:method: GET
:path: /api/v4/projects/83/repository/commits
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-type: json
origin: http://labtime.local
pragma: no-cache
private-token: [my token]
referer: http://labtime.local/?
user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/65.0.3325.181 Chrome/65.0.3325.181 Safari/537.36
请求代码:
$.ajax({
beforeSend: function(request) {
request.setRequestHeader("PRIVATE-TOKEN", "[my token]");
},
contentType: "json",
accept:"*/*",
method:"GET",url:"https://gitlab.example.de/api/v4/projects/"+id+"/repository/commits",
success: function(data) {
panel.html(data);
}
})
请考虑尝试这个
var settings = {
"async": true,
"crossDomain": true,
"url": https://gitlab.example.de/api/v4/projects/"+id+"/repository/commits,
"method": "GET",
"headers": {
"PRIVATE-TOKEN": "TOKEN"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
注意:只需验证一次您是否在 URL 中获得了正确的值。我已经为用户测试过它并且它对我来说工作正常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句