我如何在.then函数AngularJS中访问响应标头

您知道我如何从get请求访问响应标头吗?

我有一个返回承诺的服务功能。我的控制器解析了Promise,然后在.then函数中,我需要响应头中的内容类型。

我尝试使用console.log(headers())显示的“ headers”参数,但是控制台中显示的错误“ headers()不是函数”。

我的服务:

.factory('GetResultFile',
['$http', '$q',
function ($http, $q) {

    var service = {};
    service.getResult = function(id, rid) {

        var deferred = $q.defer();

        $http
        .get('http://localhost:9999/v1/jmeter/' + id + '/results/' + rid, {cache: false})
        .then(function(data, status, headers, config) {
            if(data.status == 200) {
                console.log(data.status);
                deferred.resolve(data);
            }
            else {
                deferred.reject(data);
            }
        });
        return deferred.promise;                
    }
    return service;

}]);

控制器:

$scope.getResult = function(rid) {
        console.log($scope.id);
        GetResultFile.getResult($scope.id, rid)
        .then(function(data, headers) {
            //console.log(headers.Content-type);
            console.log(headers());
            console.log(data);
            console.log("Download succeed");
            console.log(data.status);

            var file = new Blob([data.data], {type: 'text/plain;charset=utf-8'});
            FileSaver.saveAs(file, 'test.txt');

        }, function (data) {
            console.log("Download ERROR!");
            console.log(data.status);
        })
    };              

}])

AkAk47

没有更多的信息,我只能考虑Standard之类的东西

    this.$http.get('/your/Route')
      .then(response => {
        console.log(response) // Full information
        console.log(response.data) // holds your Data
        console.log(response.config) // holds more Specific information like the Url and more
        console.log(response.headers());// Specific headers Information
        console.log(response.headers(["content-type"]));//gets the Content-Type of Header

});

通常针对角度服务和响应

响应对象具有以下属性:

  • 数据{string|Object}用转换函数转换的响应主体。

  • status{number}–响应的HTTP状态代码。

  • 标头{function([headerName])}标头获取器功能。

  • config{Object}–用于生成请求的配置对象。

  • statusText{string}–响应的HTTP状态文本。

https://docs.angularjs.org/api/ng/service/ $ http

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章