无法读取undefined的属性'protocol'

 小古 发布于 2023-01-31 14:20

在尝试从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) 

Stewie.. 37

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

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

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

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

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


Laxmikant Da.. 15

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

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有