Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Recently, I began to study Angular. So much I do not know. I'm trying to get json data from a php file to use within an Angular controller. But the data from php file does not exceed.

controllers.js:

app.controller('MainCtrl',['$scope', '$resource', '$http',
function($scope, $resource,$http) {
    $http.get('/xbmc.php').success(function(data) {
        $scope.data = data;
    });
}]);

xbmc.php:

    <?
include('sys/inc/config.inc.php');
include(SMARTY_DIR.'Smarty.class.php');
include(BASE_DIR .'sys/inc/classes.inc.php');
include(BASE_DIR .'sys/inc/init.inc.php');
include(BASE_DIR .'xbmcApi.php');
$jsonData = new xbmcApi($_GET['action']);
/**
if (MEGAKINO_LOGED)
{
**/
    $json = $jsonData->getResult();
/**
}
else 
    $json = array('authStatus' => '0');
**/     
echo json_encode($json);
?>

index.html:

<body ng-controller="MainCtrl">
    <div class="wrapper">
        <h2>{{title}}</h2>
        <div class="row">
            <div class="col-md-1 col-sm-2 col-xs-4" style="margin-top: 0.5%;" ng-repeat="item in data.items">
                <div class="image" style="margin-bottom: 1%">
                    <a data-ng-href="#!/seasons/serie/{{item.id}}">
                        <img data-ng-src="/files/series/thumb-{{item.id}}.jpg" alt=""/>
                    </a>
                </div>
                <div class="info">
                    <a data-ng-href="#!/seasons/serie/{{item.id}}">
                        <b>{{item}}</b>
                        <u>Рейтинг: {{item.rate}}</u>
                    </a>
                </div>
            </div>
        </div>
    </div>
</body>
share|improve this question
    
Is $json stores the correct output ? – Vineet yesterday
    
Add console.log(data) inside the promise function and tell us what you see. – Charlie H yesterday
    
did you test your php with browser address bar, does it work ? And do you have any error when yo use angular? – RockOnGom yesterday

2 Answers 2

up vote 0 down vote accepted

Your php code tells that your json will be build if a $_GET['action'] param is passed:

$jsonData = new xbmcApi($_GET['action']);

Yet, you are not passing any data as a query string from your angular controller. Try something like:

app.controller('MainCtrl',['$scope', '$resource', '$http',
function($scope, $resource,$http) {
    $http({
        url: '/xbmc.php', 
        method: "GET",
        params: {action: 'some_action'}
     }).success(function(data) {
        $scope.data = data;
    });
}]);
share|improve this answer
1  
you saved me !!!!!thank you very much!!! – Edward Lee yesterday
    
Glad to help you! – Lazarev Alexandr yesterday

AS @CharlieH said, check for errors on your console:

app.controller('MainCtrl',['$scope', '$resource', '$http',
function($scope, $resource,$http) {
    $http.get('/xbmc.php')
        .then(function(response) {
             $scope.data = response.data;
    })
        .catch (function(error) {
             //check for errors here
             console.log(error);
             throw error;                   
    });
}]);

Also the .success method has been deprecated. We should all be migrating to using .then and .catch. For more information on that see: Deprecation of the .success and .error methods in the $http service

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.