无法读取的未定义的属性'协议' [英] Cannot read property 'protocol' of undefined

查看:415
本文介绍了无法读取的未定义的属性'协议'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图从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请求。

您不应该设置你的头在一个单独调用 $ 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;
});

这篇关于无法读取的未定义的属性'协议'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆