admin管理员组文章数量:1291029
do you know how i can access the response header from a get request ?
I have a service function which return a promise. My Controller resolve the promise and in .then function i need the content type from the response header.
I have try to use the "headers" parameter, which i display with console.log(headers()), but the error "headers() is not a function" shown in my console.
My Service :
.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;
}]);
controller:
$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);
})
};
}])
do you know how i can access the response header from a get request ?
I have a service function which return a promise. My Controller resolve the promise and in .then function i need the content type from the response header.
I have try to use the "headers" parameter, which i display with console.log(headers()), but the error "headers() is not a function" shown in my console.
My Service :
.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;
}]);
controller:
$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);
})
};
}])
Share Improve this question edited Jan 10, 2017 at 12:43 Kai asked Jan 10, 2017 at 12:31 KaiKai 811 gold badge3 silver badges12 bronze badges 4- Could you please add the code of your service. – Manu Obre Commented Jan 10, 2017 at 12:32
- 2 Possible duplicate of How to read response headers in angularjs? – Mosh Feu Commented Jan 10, 2017 at 12:33
- i updated my answer. @MoshFeu oh, i will check the post – Kai Commented Jan 10, 2017 at 12:45
- @kai Check my Answer and accept it please If it satisfies you – AkAk47 Commented Jan 10, 2017 at 14:23
1 Answer
Reset to default 8Without some more Info I can only think about the Standard things like
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
});
Generally to Angular Service and Response
The response object has these properties:
data –
{string|Object}
– The response body transformed with the transform functions.status –
{number}
– HTTP status code of the response.headers –
{function([headerName])}
– Header getter function.config –
{Object}
– The configuration object that was used to generate the request.statusText –
{string}
– HTTP status text of the response.
https://docs.angularjs/api/ng/service/$http
本文标签: javascriptHow can i access the response header in then function AngularJSStack Overflow
版权声明:本文标题:javascript - How can i access the response header in .then function AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741517341a2382973.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论