dtutlamjasblef7982
2015-11-25 06:16
浏览 46
已采纳

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

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>

图片转代码服务由CSDN问答提供 功能建议

最近,我开始研究Angular。 我真不知道。 我正在尝试从php文件中获取json数据以在Angular控制器中使用。 但来自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:< / strong>

 &lt;?
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-&gt; getResult(); 
 /  ** 
} 
else 
 $ json = array('authStatus'=&gt;'0'); 
 ** / 
echo json_encode($ json); 
?&gt; 
  <  /pre>

index.html:

nn
<body ng-controller =“MainCtrl”&gt; 
&lt;  ; div class =“wrapper”&gt; 
&lt; h2&gt; {{title}}&lt; / h2&gt; 
&lt; div class =“r  ow“&gt; 
&lt; div class =”col-md-1 col-sm-2 col-xs-4“style =”margin-top:0.5%;“  ng-repeat =“data.items中的项目”&gt; 
&lt; div class =“image”style =“margin-bottom:1%”&gt; 
&lt; a data-ng-href =“#!/  seasons / serie / {{item.id}}“&gt; 
&lt; img data-ng-src =”/ files / series / thumb  -  {{item.id}}。jpg“alt =”“/&gt;  
&lt; / a&gt; 
&lt; / div&gt; 
&lt; div class =“info”&gt; 
&lt; a data-ng-href =“#!/ seasons / serie / {{item.id  “&gt; 
&lt; b&gt; {{item}}&lt; / b&gt; 
&lt; u&gt;Рейтинг:{{item.rate}}&lt; / u&gt; 
&lt; / a&gt; 
  &lt; / div&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongshan3806 2015-11-25 06:36
    最佳回答

    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;
        });
    }]);
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题