通过javascript从tfs获取工作项

时间:2018-03-29 07:49:52

标签: javascript powershell tfs

通过以下javascript代码从tfs获取工作项有一些问题:

var res = new XMLHttpRequest();

var body = "{'query' : \"select * from workitems where [Change Number] = 'CH-0000433' \"}" ;

res.open("POST","<SERVER_NAME>/tfs/InternalApplications/Testing%20Services/_apis/wit/wiql?api-version=1.0",true,<LOGIN>,<PASSWORD>);

res.setRequestHeader('Content-type', 'application/json');

res.send(body);

当我尝试执行此脚本时,我收到401 - 未经授权的错误

我编写了这个脚本的PowerShell模拟,它工作正常:

$q2 = """select Id from workitems where [Change Number] = 'CH-0000433' """

$res = Invoke-WebRequest <Server_Name>/tfs/InternalApplications/Testing%20Services/_apis/wit/wiql?api-version=1.0 `
-ContentType application/json `
-Credential $(Get-Credential) `
-Method Post `
-Body "{'query' : $q2}" `
-UseBasicParsing

我认为在javascript情况下我传递的凭据不正确,所以我该如何更改呢?

1 个答案:

答案 0 :(得分:0)

我已经使用下面的代码片段在TFS 2017上进行了测试,它正在运行:

var jsonObj = [{
                "op": "add",
                "path": "/fields/System.Title",
                "value": "cecetest1"
            }];

            $.ajax({
                url: 'http://TFS2017:8080/tfs/DefaultCollection/ScrumProject/_apis/wit/workitems/$Task?api-version=1.0',
                type: 'PATCH',
                contentType: "application/json-patch+json",
                data: JSON.stringify(jsonObj),
                cache: false,
                dataType: 'json',
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", "Basic " + btoa("domain\\username" + ":" + "password"));
                },
            })