基本的REST调用WordPress V2 API与Angular.js
Basic REST calls to WordPress V2 API with Angular.js
我正在尝试在WordPress 4.4.1中使用新的V2 API和Angular进行基本查询。也许有人可以帮助我理解为什么 URL /wp-json/wp/v2/posts
给出带有 404 的 JSON 响应。
浏览到该 URL 会给我这样的 JSON:
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
这是我用来制作它的JavaScript。获取
var base = 'http://localhost:8888/recruitler';
var posts = '/wp-json/wp/v2/posts';
var user = '/wp-json/wp/v2/users/'; // append user id
var s = '/wp-json/wp/v2/posts?filter[s]='; // append search term
// basic HTTP call with Angular
var app = angular.module('app', ['ngRoute'])
app.factory('myService', function($http) {
var myService = {
async: function() {
// $http returns a promise, which has a then function, which also returns a promise
var promise = $http.get( base+posts ).then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response);
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return myService;
});
app.controller('MainCtrl', function( myService, $scope ) {
// Call the async method and then do stuff with what is returned inside our own then function
myService.async().then(function(d) {
$scope.data = d;
});
});
更新:这一定是局部环境问题。实时网站工作得很好。我已经测试过,这个问题在我所有本地的 WAMP 和 MAMP 开发站点上仍然存在。重新启动服务器并/或检查此答案使其正常工作。
工厂看起来是正确的,根据 rest-api 文档,您还需要漂亮的永久链接插件才能使用 rest-API 插件使用自定义 URL 重写 https://wordpress.org/plugins/rest-api/installation/
相关文章:
- 将复杂对象从angular js传递到web api,它总是返回404
- 如何在使用loopback REST API时更改gulp-angular项目的端口号
- 图像未加载javascript、html、angular、imdb-api
- 使用Angular JS for WordPress JSON API使用JSON操作数据
- 使用Angular获取数据API调用时出错
- 在一个服务中引用多个API调用(Angular)
- angular js - 指定 Rails API 的主机名
- 如何使用已经存在的 REST API 后端在 Angular 应用程序中实现登录系统
- 尝试通过 Angular 控制器使用 Google Geocode API 进行地理编码,值未定义
- Angular ng-model 将数据作为数组发送到 rails-api
- 使用Angular Resource查询具有字符串参数的REST API
- Yelp API、OAuth和Angular与JSONP只工作一次
- 当使用带有Angular JS的Mapbox API时,如何自动完成位置搜索
- 如何使用不同的API's在同一个Angular JS应用程序中具有不同的身份验证标头
- Angular Factory:实现getAll函数以使用REST-Api
- 历史API/Angular:如何返回到预先确定的URL
- angular js文本到语音转换api
- angular 2和谷歌地图api的typescript逻辑问题
- 使用Angular从API获取JSON数据
- Angular,GitHub API,返回存储库和标签