无法读取未定义的属性“协议”

在尝试从API获取数据时在控制台中获取该错误。 有人有这个问题吗?

var url = "https://api.website.com/get/?type=events&lat=" + localStorage.getItem('latitude') + "&lng=" + localStorage.getItem('longitude') + "&distance=50"; $http({ headers: { 'Content-type': 'application/json' } }) $http.get(url).success(function (events) { $scope.events = events; }); 

错误:

 TypeError: Cannot read property 'protocol' of undefined at Gb (http://localhost:38772/www/js/plugins/angular.min.js:114:238) at s (http://localhost:38772/www/js/plugins/angular.min.js:65:249) at new EventsController (http://localhost:38772/www/js/angular.js:4:5) at d (http://localhost:38772/www/js/plugins/angular.min.js:30:452) at Object.instantiate (http://localhost:38772/www/js/plugins/angular.min.js:31:80) at http://localhost:38772/www/js/plugins/angular.min.js:61:486 at http://localhost:38772/www/js/plugins/angular.min.js:49:14 at q (http://localhost:38772/www/js/plugins/angular.min.js:7:380) at E (http://localhost:38772/www/js/plugins/angular.min.js:48:382) at f (http://localhost:38772/www/js/plugins/angular.min.js:42:399) 

您正在发出格式错误的$ http请求。

你不应该设置你的标题在$http的单独调用。 调用$http()将实际发出请求,但是由于您只是使用标头(没有url或方法)对其进行configuration,所以会引发错误(如预期的那样)。

如果你想设置你的头文件,你需要将一个自定义configuration对象作为第二个parameter passing给你的$http.get()调用:

 $http.get(url, { headers: { 'Content-type': 'application/json' } }).success(function (events) { $scope.events = events; }); 

如果请求中出现错误,则会发生此错误。 如果您将url设置为未定义,无效方法或无效内容types,请求对象的任何错误都会抛出此错误。