0

I'm trying to get a basic query going with the new V2 API and Angular in WordPress 4.4.1. Perhaps someone can help me understand why the URL, /wp-json/wp/v2/posts, gives a JSON response with a 404.

Browsing to that URL gives me JSON like this:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

And here is the JavaScript I'm using to make that .GET

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: This must be a local environment issue. Live websites work just fine. I've tested and this issue persists on all my local WAMP and MAMP dev sites. Restarting the server and or checking this answer got it working.

Community
  • 1
  • 1
Ben Racicot
  • 5,332
  • 12
  • 66
  • 130

1 Answers1

1

the factory looks right, according to the rest-api docs you need pretty permalinks plugin as well in order to rest-api plugin use custom url rewrites https://wordpress.org/plugins/rest-api/installation/

mr_sudaca
  • 1,156
  • 7
  • 9
  • Oh wow you need that plugin as well? The API is kind of a mess huh? Now I'm getting `GET http://localhost:8888/recruitler/wp-json/wp/v2/posts net::ERR_EMPTY_RESPONSE` – Ben Racicot Jan 23 '16 at 13:51
  • try with 127.0.0.1 instead of localhost – mr_sudaca Jan 23 '16 at 16:47
  • I've been playing with the URL and researching for 4 hours now... I have no idea how to use the API. – Ben Racicot Jan 23 '16 at 21:35
  • I just installed wordpress on my laptop, I enabled numeric permalinks and added a sample .htaccess from https://codex.wordpress.org/htaccess (I used the Subfolder Example). If you have your wp installation in a subfolder you new to change the RewriteBase value. On my .htaccess it looks like this: RewriteBase /wordpress/ – mr_sudaca Jan 24 '16 at 16:18