[英]Basic REST calls to WordPress V2 API with Angular.js
我正在嘗試使用WordPress 4.4.1中的新V2 API和Angular進行基本查詢。 也許有人可以幫助我理解為什么/wp-json/wp/v2/posts
URL會給出帶有404的JSON響應。
瀏覽到該URL會給我這樣的JSON:
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
這是我用來制作.GET的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插件使用自定義網址重寫https://wordpress.org/plugins/rest-api/installation/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.