Cannot read property 'protocol' of undefined

前端 未结 2 1490
爱一瞬间的悲伤
爱一瞬间的悲伤 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;
    });
    
    0 讨论(0)
  • 2021-01-03 18:19

    This error occurs when something goes wrong in request, for ex. if you set url as undefined, invalid method, or invalid content type, anything wrong with request object will throw this error.

    0 讨论(0)
提交回复
热议问题