在TFS 2015 Update 3中,一切正常,沒有問題。我曾經使用npm包request消耗所有apis,沒有任何問題。 使用jQuery下面的調用也總是正確完成:Ajax調用TFS 15 Rest Api升級後停止工作
//this used to work in 2015 Update 3
var request = {
url: "https://my_server/tfs/DefaultCollection/_apis/projects?api-version=2.0",
type:'GET',
contentType: "application/json",
accepts: "application/json",
dataType: 'json',
data: JSON.stringify(data),
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("my_username:my_password"));
}
};
$.ajax(request);
升級到TFS 15 RC2上述機制不再工作之後。服務器始終返回401 - 未經授權的錯誤。
測試通過捲曲相同的呼叫,一切都很順利:
//this works well
curl -u my_username:my_password https://my_server/tfs/DefaultCollectiopis/projects?api-version=2.0
但是當我再次嘗試發送憑據在標題中失敗了,這樣的事情:
//this will fail
curl https://my_server/tfs/DefaultCollection/_apis/projects?api-version=2.0 \
-H "Accept: application/json" \
-H "Authorization: Basic eNjPllEmF1emEuYmFuNppOUlOnVuZGVmaW5lZA=="
同401 - 未經授權的錯誤。
我試圖建立我的個人訪問令牌,因爲它包含在TFS 15 RC2,並做一個測試指示here
$(document).ready(function() {
$.ajax({
url: 'https://my_server/defaultcollection/_apis/projects?api-version=2.0',
dataType: 'json',
headers: {
'Authorization': 'Basic ' + btoa("" + ":" + myPatToken)
}
}).done(function(results) {
console.log(results.value[0].id + " " + results.value[0].name);
});
});
,它也失敗。但是,更換myPatToken對我的實際密碼,通過我的用戶名以及之後,則請求成功完成:
//everything works correctly with the headers like this
headers: {
'Authorization': 'Basic ' + btoa("my_username:my_password")
}
在簡單地說,事情錯了,當我設置這樣的(使用jQuery)標題:
//this fails
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("my_username:my_password"));
}
看起來像包npm request,這是我使用的,也可能使用beforeSend屬性或類似的,它的失敗。
//this used to work with 2015 Update 3, not anymore after upgrading to 15 RC2
var options = {
url: 'https://my_server/defaultcollection/_apis/projects?api-version=2.0',
method:'GET',
headers: {
'Authorization': "Basic " + btoa("my_username:my_password")
},
json: data
};
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
console.log(response);
} else {
console.log(error);
}
});
它使我認爲這可能是在IIS配置中的東西,但基本身份驗證已正確配置。有沒有辦法使用軟件包請求來實現這個功能? 升級後IIS配置中有什麼變化?