尝试从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。Call to $http()实际上会发出请求,但是由于您仅使用标头配置了请求(没有url或方法),因此它会向您抛出该错误(如预期的那样)。
$http
$http()
如果要设置标题,则需要通过将自定义配置对象作为第二个参数传递给$http.get()调用来实现:
$http.get()
$http.get(url, { headers: { 'Content-type': 'application/json' } }).success(function (events) { $scope.events = events; });