简体   繁体   English

$ http-标头不返回-AngularJS

[英]$http - Headers not returning - AngularJS

I have an Angular Service I'm trying to set up to retrieve data from a node server that serving JSON files to the application. 我正在尝试设置Angular Service,以从将JSON文件提供给应用程序的节点服务器中检索数据。 I've checked the node server URL's and they're all outputting JSON like they should be, but Angular appears to be unable to get them due to the HTTP headers. 我检查了节点服务器的URL,它们都输出了应有的JSON,但是由于HTTP标头,Angular似乎无法获取它们。 From some googling this looks like a common problem, I'm just not sure how or even if there's a work around? 从一些谷歌搜索来看,这似乎是一个常见问题,我不确定是否可以解决,甚至可以解决吗?

eventsApp.factory('eventData', function($http, $log) {
    return {
        getEvent: function(successcb) {
            $http({method: 'GET', url: 'http://localhost:8000/data/event/1'}).
                success(function(data, status, headers, config){
                    successcb(data);
                }).
                catch(function(data, status, headers, config){
                    $log.warn(data, status, headers, config);
                })
        }
    }
});

In the console I'm getting the following warning from the catch: 在控制台中,我从警告中得到以下警告:

Object { data: null, status: 0, headers: headersGetter/<(), config: Object, statusText: "" } undefined undefined undefined 对象{数据:空,状态:0,标题:headersGetter / <(),配置:对象,statusText:“”}未定义未定义未定义

From the documentation 文档中

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

The response object has these properties: 响应对象具有以下属性:

  • data – {string|Object} – The response body transformed with the transform functions. data – {string|Object} –使用转换函数转换的响应主体。
  • status – {number} – HTTP status code of the response. status – {number} –响应的HTTP状态代码。
  • headers – {function([headerName])} – Header getter function. 标头– {function([headerName])} –标头获取函数。
  • config – {Object} – The configuration object that was used to generate the request. config – {Object} –用于生成请求的配置对象。
  • statusText – {string} – HTTP status text of the response. statusText – {string} –响应的HTTP状态文本。

So you should be able to get the interested header using: 因此,您应该能够使用以下方法获取感兴趣的标头:

response.headers('header-name')

Aslo according to your response's log: the result.data is null. 根据您的响应日志记录result.dataresult.data为null。 So you should check the Network tab of Chrome Dev Tools and examine the aqtual http response from the server. 因此,您应该检查Chrome Dev Tools的“ Network标签,并检查服务器发出的HTTP响应。

The status 0 indicates the possible CORS problem. status 0表示可能存在CORS问题。 So if your angular application is hosted on another host or port than server to which you trying to send the http request (localhost:8000 in your case), then you shoud configure the SERVER to allow CORS requests. 因此,如果您的角度应用程序托管在您尝试向其发送http请求的服务器之外的其他主机或端口上(在本例中为localhost:8000),则应将SERVER配置为允许CORS请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM