I’m struggling to get my AngularJS frontend to properly communicate with my PHP Slim backend. I keep getting HTTP 200 responses but the content is empty or garbled (119 empty characters).
My AngularJS controller attempts:
First approach using direct $resource:
app.controller('DeviceController', function($scope, $modal, $resource) {
var deviceId = 1;
var DeviceResource = $resource('http://example.com/api/phones/' + deviceId);
$scope.deviceData = DeviceResource.get();
});
Second approach with a service:
app.controller('DeviceController', function($scope, $modal, PhoneService) {
$scope.deviceData = PhoneService.get({id: 1});
});
app.service('PhoneService', function($resource) {
return $resource('api/phones/:id', {}, {
modify: {method: 'PUT'}
});
});
My PHP Slim backend:
require 'Slim/Slim.php';
$application = new Slim();
$application->get('/', 'getTrendingDevices');
$application->get('/new_phones', 'getLatestPhones');
$application->get('/phones', 'getAllPhones');
$application->get('/new_phones/:id', 'getDeviceDetails');
$application->get('/phones/:id', 'getDeviceDetails');
$application->run();
function getDeviceDetails($deviceId) {
$query = "SELECT * FROM devices";
try {
$database = getDatabaseConnection();
$statement = $database->query($query);
$results = $statement->fetchAll(PDO::FETCH_OBJ);
$database = null;
echo json_encode($results);
} catch(PDOException $error) {
echo '{"error":{"message":' . $error->getMessage() . '}}';
}
}
The database connection works fine when I test it directly through phpMyAdmin. My table has one row with 21 columns so there should be data returned. Even when I try to return a simple test string, it comes back as corrupted data.
What could be causing this issue with the data transmission between AngularJS and Slim?