无法读取的未定义的属性'协议'属性、协议、未定义

2023-09-10 17:11:34 作者:水:淼淼淼、水贴专业户、水漫金山

试图从API获取数据时,遇到了这个错误控制台。之前,任何人有这个问题?

  VAR URL =htt​​ps://api.website.com/get/?type=events&lat=+ localStorage.getItem('纬度')
+&放大器; LNG =+ localStorage.getItem('东经')+&放大器;距离= 50;

$ HTTP({
    标题:{
        内容类型:应用/ JSON
    }
})

$ http.get(URL).success(函数(事件){
    $ scope.events =事件;
});
 

错误:

 类型错误:无法读取的未定义的属性'协议'
在GB(HTTP://本地主机:38772 / WWW / JS /插件/ angular.min.js:114:238)
在s(HTTP://本地主机:38772 / WWW / JS /插件/ angular.min.js:65:249)
在新的EventsController(HTTP://本地主机:38772 / WWW / JS / angular.js:4:5)
在d(HTTP://本地主机:38772 / WWW / JS /插件/ angular.min.js:30:452)
在Object.instantiate(HTTP://本地主机:38772 / WWW / JS /插件/ angular.min.js:31:80)
在http://本地主机:38772 / WWW / JS /插件/ angular.min.js:61:486
在http://本地主机:38772 / WWW / JS /插件/ angular.min.js:49:14
在Q上(http://本地主机:38772 / WWW / JS /插件/ angular.min.js:7:380)
在E(HTTP://本地主机:38772 / WWW / JS /插件/ angular.min.js:48:382)
在f(HTTP://本地主机:38772 / WWW / JS /插件/ angular.min.js:42:399)
 

解决方案

你发出一个畸形的$ http请求。

急 笔记本无线网络显示已连接,可是无法上网,检测提示DNS服务器未响应怎么解决

您不应该设置你的头在一个单独调用 $ HTTP 。打电话到 $ HTTP()将实际发出的请求,但因为你只用头(没有URL或方法)配置,它在你抛出的错误(如预期)。

如果你想设置你的头,你会想这样做,通过传递一个自定义的配置对象作为第二个参数的 $ http.get()电话:

  $ http.get(URL,{
  标题:{
    内容类型:应用/ JSON
  }
}),成功(函数(事件){
  $ scope.events =事件;
});
 

Getting that error in console when trying to get data from a API. Anybody have this issue before?

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;
});

error:

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) 

解决方案

You're issuing a malformed $http request.

You are not supposed to set your headers in a separate call to $http. Call to $http() will actually issue the request, but since you configured it with just the header (no url or method), it throws that error at you (as expected).

If you want to set your header you'll want to do that by passing a custom config object as a second parameter to your $http.get() call:

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