javascript - 从 Angular Controller 中的 php 文件获取 JSONP 数据

标签 javascript php angularjs json

最近开始学习Angular。这么多我不知道。我正在尝试从 php 文件获取 json 数据以在 Angular Controller 中使用。但php文件中的数据不超过。

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>

最佳答案

您的 php 代码告诉您,如果传递 $_GET['action'] 参数,则将构建您的 json:

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

但是,您没有将任何数据作为查询字符串从 Angular Controller 传递。尝试这样的事情:

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;
    });
}]);

关于javascript - 从 Angular Controller 中的 php 文件获取 JSONP 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909662/

相关文章:

javascript - JavaScript 中 "decorator function"和 "decorator design pattern"有什么区别?

php - Yii 不是自动的,包括 jquery

PHP、MySQL 和 XML = 乱码 HTML 输出

html - 将字符串附加到 ng-model 的开头

angularjs - 使用selenium java需要从angularjs检索数据

javascript - PDF JavaScript 不再在 Edge 中运行

javascript - 是否可以在浏览器中使用 javascript 对用户系统进行基准测试

javascript - 如何在 Python 中抓取包含动态内容(由 JavaScript 创建)的页面?

php - Mysql 表在云 9 中未显示为 html 表

javascript - AngularJS 和异步调用