Cannot read property 'protocol' of undefined

前端 未结 2 1491
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 17:19

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&la         


        
2条回答
  •  没有蜡笔的小新
    2021-01-03 18:10

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

提交回复
热议问题