2016-01-23 67 views
0

我試圖在WordPress 4.4.1中使用新的V2 API和Angular進行基本查詢。也許有人可以幫助我理解爲什麼URL,/wp-json/wp/v2/posts,給出了404使用Angular.js進行WordPress V2 API的基本REST調用

瀏覽到該URL的JSON響應給我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; 
    }); 
}); 

UPDATE: 這必須是本地的環境問題。現場網站工作得很好。我已經測試過,並且這個問題在我所有的本地WAMP和MAMP開發站點上都存在。重新啓動服務器和或檢查this answer得到它的工作。

回答

1

工廠看起來正確,根據你需要漂亮的永久鏈接插件,以及以REST的API插件使用自定義網址重寫https://wordpress.org/plugins/rest-api/installation/

+0

哇的REST的API文檔你需要的插件呢?該API是一種混亂吧?現在我得到'GET http:// localhost:8888/recruitler/wp-json/wp/v2/posts net :: ERR_EMPTY_RESPONSE' – BenRacicot

+0

嘗試使用127.0.0.1而不是本地主機 –

+0

我一直在玩URL和現在研究了4個小時......我不知道如何使用API​​。 – BenRacicot

相關問題