AngularJS $ http日期标题

Yan*_* Li 3 angularjs

我正在尝试从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);
  })
});
Run Code Online (Sandbox Code Playgroud)

我似乎无法检索Date标题,虽然当我在chrome工具上检查时,日期标题就在那里.

moh*_*ias 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;
      })
Run Code Online (Sandbox Code Playgroud)

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"
}
Run Code Online (Sandbox Code Playgroud)

访问日期:

headers.date
Run Code Online (Sandbox Code Playgroud)

因为这是对facebook api的CORS请求:响应头只包含

Content-Type
Last-modified
Content-Language
Cache-Control
Expires
Pragma
Run Code Online (Sandbox Code Playgroud)

问题是因为Access-Control-Allow-Headers请求Header 丢失了.要解决此问题,我们需要Access-Control-Allow-Headers: *在run方法中添加请求标头