我用这个职位测试了代码,并对其进行了一些修改以供使用。但是,我无法从我的博客的API中获得JSON对象,这个API是我使用WordPress JSON插件生成的。
当我试图从我的博客中操作JSON时(上面提到过),同样的代码也适用于w3c示例提供的其他url?
请提供你的建议。
我在.html
文件中使用下面的代码,而不是在WordPress环境中。
==== 角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>
我在检查了所有的在线解决方案https://stackoverflow.com/a/26898082/1841647和http://www.ivivelabs.com/blog/fix-cross-domain-ajax-request-angularjs-cors/后提出了这个问题,但是对我来说没有什么效果。
为您的方便创建JSFiddle:http://jsfiddle.net/236gdLnt/
发布于 2015-03-28 10:37:26
这是一个跨领域的问题。您可以通过使用JSONP查询第一个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中。
https://stackoverflow.com/questions/29320899
复制