我是TFS的新手。下面是我试图检索项目工作项的代码,我得到如下错误。如果我错过了什么,请建议我。我正在使用TFS onpremise。
{" $ ID":" 1&#34 ;, "的InnerException":空, " message":"您必须在请求正文中传递查询对象。", " typeName":" Microsoft.VisualStudio.Services.Common.VssPropertyValidationException,Microsoft.VisualStudio.Services.Common,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a", " typeKey":" VssPropertyValidationException""的errorCode":0," EVENTID":3000}
var resturl = "tfs Server URL/tfs/collectionname/_apis/wit/wiql?api-version=1.0"
var query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Feature' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc";
$.ajax({
"url": resturl,
"type": "POST",
"Content-type": "application/json",
body: JSON.stringify(query),
headers: {
"Accept": "application/json"
},
success: function (data) {
alert('success');
},
error: function (error) {
alert('failure');
alert(error.statusText);
alert(error.responseJSON.message);
}
});
[编辑]:我可以从以下代码中检索REST API中的项目列表。
var resturl = "tfs Server URL/tfs/collectionname/_apis/projects?stateFilter=All"
$.ajax({
"url": resturl,
"type": "GET",
"Content-type": "application/json",
headers: {
"Accept": "application/json"
},
success: function (data) {
alert('success');
},
error: function (error) {
alert('failure');
alert(error.statusText);
alert(error.responseJSON.message);
}
});
答案 0 :(得分:1)
您使用的格式错误,private String arrayToJson(ArrayList<HashMap<String, String>> arrayList) {
//Coverting Arraylist to JsonArray
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for (HashMap<String, String> data : arrayList) {
JSONObject jsonObject = new JSONObject(data);
jsonList.add(jsonObject);
}
JSONArray json_array= new JSONArray(jsonList);
String json_string= json_array.toString();
}
是字符串格式而不是 json格式。这就是您收到错误var query
您可以参考此官方教程 Work item query language
"You must pass a query object in the body of the request."
所以你需要使用bleow格式
POST https://{instance}/DefaultCollection/[{project}/]_apis/wit/wiql?api-version={version}
Content-type: application/json
{
"query": string
}