I'm doing an add-on for Firefox and this is powered by json that comes from an ajax request, which I do via http's get method.
When I print json by calling the direct address on the screen, it normally displays, but when I make the request from the angle, it accuses json of being syntax error.
Follow the scripts
File that is generating Json
I've changed the script a bit, but now it accuses me that what I'm getting is not an object
How was the class:
<?php
header('Content-Type: application/json');
class ListaDeLinks{
public $links = array();
function __construct(){ }
function addLink($nome,$link){
$this->links[$nome] = $link;
}
}
$l = new ListaDeLinks();
$l->addLink('google','http://www.google.com');
$l->addLink('bing','http://www.bing.com');
$l->addLink('yahoo','http://www.yahoo.net');
$rest = json_encode($l);
echo $rest;
?>
File that receives Json
var lilink = angular.module('lilinkApp',[]);
lilink.controller('lilinkController',lilinkController);
lilinkController.$inject = ['$scope','$http'];
function lilinkController($scope,$http){
$scope.lilink = "lilink";
$http.get('http://localhost/webS/lkrest.php')
.then(function(data){
$scope.links = data;
},function(errData){
console.log(errData);
});
}
Presentation
<!DOCTYPE html >
<html lang="pt-BR" ng-app="lilinkApp">
<head>
<meta charset="UTF-8">
<title>LiLink</title>
<link rel="stylesheet" href="../css/bootstrap.css">
<script src="../js/jquery.js"></script>
<script src="../js/angular.js"></script>
<script src="../js/bootstrap.js"></script>
<link rel="stylesheet" href="../css/lilink.css">
<script src="../js/lilink.js"></script>
</head>
<body>
<main ng-controller="lilinkController" class="container-fluid">
<nav class="row">
<div class="col-sm-12">
</div>
</nav>
<div class="row">
<section style="background-color:#afa" id="links" class="col-sm-8" >
{{links}}
</section>
<section style="background-color:#faa" id="notificacoes" class="col-sm-4">
{{lilink}}
</section>
</div>
</main>
</body>
</html>