我正在使用wordpress json api,ionic和angularjs创建一个混合Android应用程序。我只是坚持一件事:我无法列出特定类别的帖子。我得到了类别列表但无法传递一些参数。这是代码
controller.js
var app = angular.module('tsApp', ['ionic'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/',
views: {
home: {
templateUrl: 'index.html',
controller: 'TSController'
}
}
})
.state('category', {
url: '/category/:slug',
params: {
category: null //stores everything of catergory: slug, name etc.
},
views: {
category: {
templateUrl: 'category.html',
controller: 'CatsPostsCtrl'
}
}
});
$urlRouterProvider.otherwise('/');
})
// Home Controller
app.controller('TSController', function($scope, $http) {
$scope.categories = [];
$http.jsonp('http://localhost/khaliddev/azkarserv/api/get_category_index?callback=JSON_CALLBACK')
.success(function (data1) {
//console.log(data1);
$scope.categories = data1.categories; // response data
//console.log( $scope.categories);
}).error(function (data1) {
//console.log("BLOG failed");
});
});
// Cat Controller
app.controller('CatsPostsCtrl', function($scope, $http, $stateParams) {
console.log($stateParams);
// You can change this url to experiment with other endpoints
//http://localhost/khaliddev/azkarserv/api/?json=get_category_posts&slug=azkar-for-morning&status=publish
var postsApi = 'http://localhost/khaliddev/azkarserv/api/?json=get_category_posts&slug=' + $stateParams.slug + '&status=publish=JSON_CALLBACK';
$scope.category = $stateParams.category.name;
// This should go in a service so we can reuse it
$http.jsonp( postsApi, {cache:true} ).
success(function(data, status, headers, config) {
$scope.posts = data;
console.log( data );
}).
error(function(data, status, headers, config) {
console.log( 'Post load error.' );
});
});
TSController
显示所有类别,但CatsPostsCtrl
应显示其不属于的所有类别的帖子。它显示我在consle(错误404),任何帮助表示赞赏。谢谢!
index.html:
<div class="page-content">
<nav class="dashboard-menu">
<div class="row text-center" >
<div class="col-33" ng-repeat="cat in categories">
<a href="category/{{cat.slug}}" class="menu-link">
<span class="icon-folder-plus"></span>
<span>{{cat.title}}</span>
</a>
</div>
</div>
</nav>
</div>
答案 0 :(得分:0)
我认为你在这里有错误,也许你忘了“&amp; callback =”
&status=publish=JSON_CALLBACK
你知道当你使用JSONP时你的json应该被包裹吗?
我曾经遇到过同样的问题,网址很好,Chrome的预览显示了正确的JSON,但状态是404.当我将json包裹在angular.callbacks._0()附近时,它工作了
如果您有后端服务,我建议您只使用“$ http.get”或“$ http.post”