2016-07-22 81 views
0

作为标题,我想捕获浏览器发送的http响应。
假设重定向到“http://domain/api/something”,实际上是一个GET请求到“http://domain/api/something”,它返回一个JSON数据。
如何使用AngularJs首次加载数据?angularjs如何捕获浏览器发送的http响应数据

+0

请详细描述你要完成什么,也使在这样做了一些尝试。 –

+0

假设用户在浏览器中输入我的页面“http:// domain/api/something”,那么我的服务器返回一个JSON数据/一个Html页面等等。在Html页面的情况下,浏览器将呈现它。问题是,我怎样才能以文本/ HTML格式获得Html页面而不发送其他请求。 –

回答

0

使用$ http服务如下。

$http.get(
    'http://domain/api/something' 
).then(function successCallback(response) { 
    $scope.data = JSON.parse(response.data); 
}, function errorCallback(response) { 
    // error handler 
}); 

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

2

您应该修改代码如下

app.service('feedbackService', function ($http) { 
    this.getFeedbackPaged = function() { 
     return $http.get('http://domain/api/something'); 
    }; 
}); 

app.controller('feedbackController', function ($scope, feedbackService, $filter) { 
    // Constructor for this controller 
    init(); 

    function init() { 
     feedbackService.getFeedbackPaged().then(function(data){ 
      $scope.feedbackItems=data; 
     }); 
    } 
}); 
+0

通过这种方式,我会发送另一个请求,而不是发现浏览器已发送的响应,不是吗? –

+0

请给予更多描述。一旦你加载了init();它将调用http GET方法,响应将存储在$ scope.feedbackItems中。谢谢 –