I want to send a get request to my own Server via Angular. The Server is deployed on Heroku. I just want to receive the json data from my Server. The problem is, that I receive an error everytime I try to get the json data. When I use a different URL (like this: https://httpbin.org/get ) everything works fine. My Heroku URL: http://webservice-team-3.herokuapp.com/1
Client Code:
var app = angular.module('app', [])
app.controller('mainController', function($scope, $http) {
$scope.formData = {};
//$scope.messageData = {};
$scope.test ="hallo";
// Get all messages
$http.get('http://webengserver.herokuapp.com/Matthias')
.success(function(data) {
$scope.messageData = data;
console.log(data);
})
.error(function(error) {
$scope.messageData = "error";
console.log('Error: ' + error);
});
});
<!Doctype html>
<html ng-app="app">
<head>
<title>Webservice-Team-3-Client</title>
<style>
[ng\:cloak], [ng-cloak], .ng-cloak {
display: none;
}
</style>
</head>
<body>
<div ng-controller="mainController">
<p>{{messageData}}</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Here is the code from the Server, which handles the http get:
router.get('/:user_id', function(req, res) {
var results = [];
var id = req.params.user_id
// Get a Postgres client from the connection pool
pg.connect(connectionString, function(err, client, done) {
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({ success: false, data: err});
}
// SQL Query > Select Data
var query = client.query("SELECT * FROM users where userid=($1) ORDER BY userid ASC;", [id]);
// Stream results back one row at a time
query.on('row', function(row) {
results.push(row);
});
// After all data is returned, close connection and return results
query.on('end', function() {
done();
return res.json(results);
});
});