TypeError:无法调用方法'获取'的未定义

TypeError: Cannot call method 'get' of undefined

本文关键字:未定义 获取 方法 调用 TypeError      更新时间:2023-09-26

我使用AngularJs和Protractor测试api数据,并尝试使用restapi从服务器获取响应,然后在控制台上显示这些数据

Bellow是我的密码

我的服务文件:

module.exports = {getResponse : function ($http){
    $http.get('http://echo.jsontest.com/title/ipsum/content/blah').
      success(function(data, status, headers, config) {
          console.log(data)
          var jsonData = JSON.stringify(data);
          console.log(jsonData);
          // this callback will be called asynchronously    
          // when the response is available
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    }   
};    

测试用例文件:

 var helper = require('./helper');
 describe('A test suite', function() {  
   it('does something to myService', function() {
        var result = helper.getResponse();  
        console.log(result);    
   }); 
  });

提前感谢。。。

您的服务模型中包含$http了吗?

angular.module('app').service('YourService', ['$http', function($http) {
// your code
}]);