Request to API using Angularjs

Asked

Viewed 328 times

1

I am still novice in development using Angularjs and am having problems accessing an API:

.controller('RegulamentoCtrl', function($scope, $state, $http, $ionicPopup, AuthService) {

    $http.get('http://rest-service.guides.spring.io/greeting')
    .then(function(response) {
        alert('teste');
    }, function(err) {
        alert(err)
    });
})

While trying to access this service, which returns me a JSON, I get the following error message:

Error: Unexpected request: GET http://rest-service.guides.spring.io/greeting No more request expected

  • There’s a mistake there or you’re calling one alert inside the other?

  • I need to communicate to an external API. Of course it won’t be this one. But I have to understand why it returns this error. To then test my API. I am researching and seeing something about $httpBackend.whenGET, but this is unfamiliar to me. So I’m running some tests. But so far nothing! :/

  • @Rick Wolff This from Alert I had already tidied up here and forgot to correct in question tb. But that’s not the problem!

1 answer

3


In the end I discovered the problems! :)

The problem is I’m using the ngMockE2E to process requests locally (for access control).

angular.module('starter', ['ionic', 'ngMockE2E'])

Therefore, my API request was not passed to the server. To solve the problem I only had to say to the ngMockE2E pass my request to the server:

.run(function($httpBackend){

  ...

  $httpBackend.whenGET('http://rest-service.guides.spring.io/greeting').passThrough();

 // configuração básica mock
  $httpBackend.whenGET(/templates\/\w+.*/).passThrough();
 })

Font of the reply: https://stackoverflow.com/questions/14761045/jasmine-tests-angularjs-directives-with-templateurl

Browser other questions tagged

You are not signed in. Login or sign up in order to post.