Integration (angularJS) with random phrases API

1

I'm trying to use angularJS to pull data from the following API that generates random phrases: link

Here is a snippet of the code I am using:

$http({
    method: 'GET',
    url: ' http://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=json&lang=en'
    }).then(function successCallback(response) {
       alert("Teste A!");
    }, function errorCallback(response) {
       alert("Teste B!");
});

However, I'm not getting any response (no alerts are executed), let alone pulling the data I want: the phrase and the author. How can I solve this?

    
asked by anonymous 28.08.2016 / 00:33

1 answer

1

This is because the api.forismatic.com server requires CORS, or that you use JSONP. The following functional example is an implementation of the second type:

var app = angular
.module("exemplo", [])
.controller("exemploController", function($scope, $http) {

  var url = "http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=JSON_CALLBACK";

  $http.jsonp(url)
  .success(function(data){
console.log(data);
    $scope.quote = data;

  });
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="exemplo" ng-controller="exemploController">
  <span style='font-style:italic' ng-bind="quote.quoteText"></span> - <span ng-bind="quote.quoteAuthor"></span>
</div>
    
28.08.2016 / 00:59