从Angular Controller中的php文件获取JSONP数据
GET JSONP data from php file in Angular Controller
最近,我开始学习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:
<?
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']);
然而,您并没有将任何数据作为查询字符串从角度控制器中传递。试试类似的东西:
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;
});
}]);
AS@CharlieH说,检查控制台上的错误:
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;
});
}]);
此外,.success
方法也已被弃用。我们都应该迁移到使用.then
和.catch
。有关更多信息,请参阅:弃用$http服务
相关文章:
- 在.js文件上使用Ajax获取JSONP
- $http.获取和$http.JSONP未能获得Google API响应
- 获取jsonp时出现授权标头错误
- 在没有jsonp的情况下从另一个域获取数据(json格式)
- 使用原始JavaScript获取JSONP
- 通过Yahoo API YQL- Javascript获取JSONP数据
- 使用 jQuery 和 jsonp (vimeo api) 按键获取选定的 json 值
- AngularJS在从HTTP返回的JSON中获取语法错误.JSONP.
- 用于 json 和 jsonp 的 $.ajax 和 $.getJSON 无法正确获取来自第三方服务器的响应
- Ajax / jQuery 获取整个 JSONP-Result-String
- JSONP 从 WCF 服务获取缓存响应时出现问题
- 如何使用ajax jsonp将数据从一个域获取到另一个域
- 请求失败时,无法在JSONP代理回调中获取响应
- 从Angular Controller中的php文件获取JSONP数据
- 使用 Angular.js获取 JSONP
- 从ajax jsonp回调中获取列表
- 使用jsonp获取400(错误请求)
- 从JSONP javascript获取数据
- 通过Angular从Firebase主机获取JSONP文件
- 开放天气图API,无法获取JSON,但无法获取JSONP,无法进行异步调用