Integration (Angularjs) with Random Sentence API

Asked

Viewed 994 times

1

I am trying to use Angularjs to pull data from the following API that generates random phrases: http://forismatic.com/en/api/

Follows excerpt from the code I’m using:

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

However, I’m not getting any answers (no Alert runs), let alone pulling the data I want: the sentence and the author. How can I fix this?

1 answer

1

This is because the.forismatic.com api server requires CORS, or that you use JSONP. The functional example below 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>
<div ng-app="exemplo" ng-controller="exemploController">
  <span style='font-style:italic' ng-bind="quote.quoteText"></span> - <span ng-bind="quote.quoteAuthor"></span>
</div>

Browser other questions tagged

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