I'm new to Angular JS. I'm able to call a php file and get the data. But, now the scenario is that I need to call a particular method from a php and get the data using "$http.get". Take a look at my code, whether I'm calling the method in the correct way or not?
Angular Code
// Ajax call for listing countries.
var countryPromise = $http.get("ListData.php/getCountries()");
// Create global users array.
$scope.countriesArray = [];
countryPromise.success(function(data, status, headers, config) {
for(index in data) {
alert(data[index].name);
$scope.countriesArray.push({
id:data[index].id,
name:data[index].name
});
}
});
countryPromise.error(function(data, status, headers, config) {
alert("Loading countries failed!");
});
PHP
<?php
class ListData
{
function __construct() {
// credentials of MySql database.
$username = "root";
$password = "admin";
$hostname = "localhost";
$countryData = array();
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("Angular",$dbhandle)
or die("Could not select Angular");
}
public function getCountries() {
//execute the SQL query and return records
$result = mysql_query("SELECT id,name FROM Country");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
$id = $row{'id'};
$name = $row{'name'};
$countryData[] = array('id' => $id, 'name' => $name);
}
echo json_encode($countryData);
}
} ?>