【问题标题】:Access TFS RESTful API from Angular web app从 Angular Web 应用程序访问 TFS RESTful API
【发布时间】:2016-06-15 17:33:14
【问题描述】:

我认为 TFS RESTful api 有一个错误。我正在尝试使用 Angular Web 应用程序访问它。我们的 TFS 服务器是公司内部的。这是我的代码:

        var path = 'http://tfs.mycompany.com/tfs/mycompany/_apis/wit/queries?$depth=1&$expand=all&api-version=2.2';
        var config = {  withCredentials: true };
        $http.get(path, config)
        .then(function (response) {
            $scope.resultList = response.data.d.results || [ response.data.d ];
            $scope.message = 'Found ' + $scope.resultList.length + ' item' + ($scope.resultList.length == 1 ? '':'s');
        }, function (response) {
            $scope.resultList = [];
            $scope.message = 'Error ' + response.status + ' ' + JSON.stringify(response.data);
        });

请求到达服务器,服务器以 OK 200 响应。但是,浏览器 (Chrome) 阻止了数据,并告诉我:

A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header 
when the credentials flag is true. Origin 'http://localhost' is therefore 
not allowed access. The credentials mode of an XMLHttpRequest is controlled 
by the withCredentials attribute.

请求头有Origin:http://localhost

响应头有Access-Control-Allow-Origin:*

我有什么方法可以告诉 TFS 在Access-Control-Allow-Origin不返回 *?这似乎是 TFS 中的一个严重错误,它使 RESTful api 对于 Web 应用程序几乎毫无用处!

【问题讨论】:

    标签: rest tfs


    【解决方案1】:

    您可以查看下面的Cross-origin resource sharing (CORS) 示例以在您的代码中添加授权:

    $( document ).ready(function() {
        $.ajax({
            url: 'https://fabrikam.visualstudio.com/defaultcollection/_apis/projects?api-version=1.0',
            dataType: 'json',
            headers: {
                'Authorization': 'Basic ' + btoa("" + ":" + myPatToken)
            }
        }).done(function( results ) {
            console.log( results.value[0].id + " " + results.value[0].name );
        });
    });
    

    另外,检查一下这个案例是否有帮助:

    AJAX cross domain issue with Visual Studio Online REST API

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2019-11-15
      • 1970-01-01
      • 2018-04-18
      相关资源
      最近更新 更多