使用 Angular JS for WordPress 使用 JSON 操作数据 JSON API

Manipulate Data with JSON using Angular JS for WordPress JSON API

我测试了 this post 中的代码并修改了一些以供我使用。但是我无法从我使用 WordPress JSON 插件生成的博客 API 中获取 JSON 对象。

  1. URL API 来自博客(不工作):http://teckstack.com/api/get_recent_posts
  2. URL 来自 W3C 示例(工作中):http://www.w3schools.com/website/Customers_JSON.php

我试图从我的博客(上面提到)操纵 JSON API 时卡住了,而相同的代码对 w3c 示例提供的另一个 url 有效?

请提出您的建议。

我在 .html 文件中使用以下代码,而不是在 WordPress 环境中

==== Angular JS 脚本 ====

(function() {
    var app = angular.module('tsApp', []);
    app.controller('TSController', function($scope, $http) {
        $scope.heading = [];
        $http({
            method: 'GET',
            url: 'http://teckstack.com/api/get_recent_posts'
        }).success(function(data) {
            console.log("pass");
            $scope.heading = data; // response data 
        }).error(function(data) {
            console.log("failed");
        });
    });
})();

==== HTML ====

<html ng-app="tsApp">
<body ng-controller="TSController as tsCtrl">
        <article class="main-content" role="main">
            <section class="row">
                <div class="content">
                    <div class="name-list">
                        <h1>Dummy Title</h1>
                        <ul>{{ 1+1 }} (Testing AJS is working)
                            <li ng-repeat="title in heading" class="">
                                <h3>{{title.Name}}</h3>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>
        </article>
        <script type="text/javascript" src="js/main.js"></script>
    </body>
</html>

我是在网上查了所有的答案后才提出这个问题的 和 http://www.ivivelabs.com/blog/fix-cross-domain-ajax-request-angularjs-cors/ 但是对我没有任何帮助。

为方便起见创建 JSFiddle: http://jsfiddle.net/236gdLnt/

这是一个跨域问题。您可以使用 $http.jsonp 方法通过 JSONP. Angular support rquest 获取第一个 url 数据:

$http.jsonp('http://teckstack.com/api/get_recent_posts?callback=JSON_CALLBACK')
   .success(function (data1) {
        console.log("BLOG pass");
        $scope.heading1 = data1; // response data 
    }).error(function (data1) {
        console.log("BLOG failed");
    });

确保将 callback=JSON_CALLBACK 参数添加到 url。