我试图从AngularJS $ http.get请求中检索$ http日期标题,这样我就可以获得服务器时间。
app.controller('MainCtrl', function($http,$scope) {
$scope.name = 'World';
$http({method: 'GET', url: 'http://graph.facebook.com/facebook'}).then(function(response){
console.log(response);
})
});
我似乎无法检索Date标题,但是当我在chrome工具上检查时,日期标题就在那里。
答案 0 :(得分:5)
试试这个:
$http({method: 'GET', url: 'http://graph.facebook.com/facebook'}).then(function(response){
var data = response.data,
status = response.status,
headers = response.headers(),
config = response.config;
})
headers
将包含:
headers: {
"date": "Mon, 02 Mar 2015 23:02:51 GMT",
"content-encoding": "gzip",
"server": "Apache",
"vary": "Accept-Encoding",
"content-type": "text/html",
"connection": "Keep-Alive",
"keep-alive": "timeout=10, max=500",
"content-length": "39"
}
访问日期:
headers.date
因为这是对facebook api的CORS请求:响应头只包含
Content-Type
Last-modified
Content-Language
Cache-Control
Expires
Pragma
问题是因为请求标头中缺少Access-Control-Allow-Headers
。要解决此问题,我们需要在运行方法
Access-Control-Allow-Headers: *
请求标头