无法读取undefined的属性'protocol'

use*_*304 39 javascript ajax angularjs

在尝试从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;
});
Run Code Online (Sandbox Code Playgroud)

错误:

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) 
Run Code Online (Sandbox Code Playgroud)

Ste*_*wie 37

您发出格式错误的$ http请求.

您不应该在单独的调用中设置标头$http.调用$http()将实际发出请求,但由于您只使用标头(没有网址或方法)配置它,它会向您抛出该错误(如预期的那样).

如果要设置标题,您可以通过将自定义配置对象作为第二个参数传递给您的$http.get()调用来实现此目的:

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

  • 我收到此错误是因为我粘贴了一些$ .post代码用于$ .delete和$ .delete(非常恰当)没有$ .post所做的第二个参数(数据) (2认同)

Lax*_*nge 15

当请求中出现问题时会发生此错误,例如.如果将url设置为undefined,invalid method或invalid content type,请求对象的任何错误都将引发此错误.