<!DOCTYPE html>
<html ng-app="MyApp">
<head>
<script data-require="angular.js@*" data-semver="1.4.0-rc.2" src="
+https://code.angularjs.org/1.4.0-rc.2/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="MyController">
<h1>Welcome!</h1>
<ul>
<li ng-repeat="x in names">{{x.Name + ' | ' + x.Country}}</li>
</ul>
<script>
var MyResponse = {
"records":{
"2":{"City":"México D.F.","Country":"Mexico","Name":"An
+a Trujillo Emparedados y helados"},
"1":{"Name":"Alfreds Futterkiste","Country":"Germany","C
+ity":"Berlin"}
}
}
var app = angular.module('MyApp', []);
app.controller('MyController', ['$scope', '$http', function($sco
+pe, $http) {
$scope.names = {};
$http.get("http://localhost:5000/about")
.success(function (response) {$scope.names = response.records;
+})
.error(function(err){
$scope.names = MyResponse.records;
});
}]);
</script>
</body>
</html>
Now, when I run the url in the browser, I get the
{"records":
{
"2":{"City":"México D.F.","Country":"Mexico","Name":"Ana Trujillo Emparedados y helados"},
"1":{"Name":"Alfreds Futterkiste","Country":"Germany","City":"Berlin"}
}
}
This is same I required for AJS to work but in the dancer logs it gives an error "Failed to serialize the request: hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) at /usr/local/share/perl/5.18.2/Dancer2/Serializer/JSON.pm line 34. in (eval 299) l. 1"
### App.pm Code ###
package TarView::App;
use Dancer2;
use Dancer::Exception;
our $VERSION = '0.1';
set serializer => 'JSON';
get '/' => sub {
template 'index';
};
get '/about' => sub {
my %hash = (
records => {
1 => {
Name => "Alfreds Futterkiste",
City => "Berlin",
Country => "Germany"
},
2 => {
Name => "Ana Trujillo Empared
+ados y helados",
City => "México D.F.",
Country => "Mexico"
}
}
);
return \%hash;
};
true;
As I understand '/' failed to return the JSON which is true because I don't want to return JSON from '/' it should use 'template index'.
Can anybody suggest how to accomplish this ?
|