VssPropertyValidationException:无法在 TFS 2017 中使用 REST API 获取工作项

VssPropertyValidationException : Not able to get Workitems using REST API in TFS 2017

我刚开始使用 TFS。下面是我试图执行以检索项目工作项的代码,我收到如下错误。如果我遗漏了什么,请建议我。我在本地使用 TFS。

{"$id":"1", "innerException":空, "message":"You must pass a query object in the body of the request.", "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);
            }
        });

您使用的格式错误,var query 字符串格式 而不是 json 格式 .这就是您收到错误 "You must pass a query object in the body of the request."

的原因

可以参考官方教程Work item query language

POST https://{instance}/DefaultCollection/[{project}/]_apis/wit/wiql?api-version={version}

Content-type: application/json

{
  "query": string
}

所以你需要使用bleow格式

var query = {"query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Feature' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"};