【发布时间】:2014-04-03 14:31:38
【问题描述】:
我已将 AngularJS 应用程序中的 $http > URL 参数从
$http({ method:'POST',
url:'http://localhost/api/clients.php'
}).success(function(data,status,headers,config){
deferred.resolve(data);
}).error(function(data,status,headers,config){
deferred.reject(status);
});
到
$http({ method:'POST',
url: ConfigService.Urls.Clients
}).success(function(data,status,headers,config){
deferred.resolve(data);
}).error(function(data,status,headers,config){
deferred.reject(status);
});
其中ConfigService如下:
Urls: {},
loadUrls: function(){
Urls.Clients = 'http://localhost/api/clients.php';
}
当然,我在通过 .Resolve 加载控制器之前调用 loadUrls,所以我 100% 确定 Urls.Clients 不为空。
进行此更改后,我开始收到错误:
TypeError: Cannot read property 'protocol' of undefined
at urlIsSameOrigin (http://localhost/myapp/app/lib/angular/angular.js:13710:17)
at $http (http://localhost/myapp/app/lib/angular/angular.js:7508:23)
令人困惑的是,该应用程序运行良好,除了我遇到的那个错误......有人可以告诉我我在这里做错了什么吗?以及如果应用程序已经正常运行,为什么我会收到此错误。
【问题讨论】:
-
@drew_w 不,因为是不同的情况,这里我不拆分表头
-
你能打完整的 $http 电话吗?
-
基本上我在这里看到的唯一一件事是
Urls.Clients必须以某种方式是undefined。很抱歉一直在问问题 - 但是,loadUrls()在哪里打电话?我在你的代码中没有看到。另外,如果你只是在服务中设置 URL,而不是把它放在一个函数中,那效果会更好吗? -
@drew_w ConfigService 是一项服务,这就是加载 url 的地方,然后我会在需要时将此服务注入服务/控制器中。话虽如此,loadUrls 使用路由提供程序解析加载到第一个控制器(主页)加载
标签: angularjs